Strange, this code managed to create the first two objects (root
and another):
var pmrh = PointMeasurementReportHeader.create({
evalAt: evalAt,
start: startEval,
end: endEval,
prev: prev ? { id: prev.id } : // NOTE: @db(unique)
PointMeasurementReportHeader.create({ id: 'root' })
});
But whenever I try again, I get an error
Error: Write failed: Object with same prev already exists in type PointMeasurementReportHeader for unique index: C3_2_PTMESRPTHDR_U_1 for object with id 09c18bfe-06a6-43f8-9811-7de112f7bdd0. Please change to unique values.
and no new objects are created (the first two is all that is returned by fetch
[1]).
There is no object in DB with the id
or prev
mentioned in the error.
There are no other (static, in code) writes (create
or other) but these two.
This is the start of type declaration:
@db(unique=['prev'])
entity type PointMeasurementReportHeader schema name "PTMESRPTHDR" {
I kept entity
, will see what happens if I remove it.
[1]
VLog.strIP('{x}', {x:PointMeasurementReportHeader.fetch().objs}) ==>
16:47:18.789 "[
{
"type": "PointMeasurementReportHeader",
"evalAt": "2019-02-14T16:03:55.568+01:00",
"start": "2017-01-01T00:00:00.000",
"end": "2017-01-08T00:00:00.000",
"prev": {
"id": "root"
},
"id": "bb67ce36-e145-4117-b524-7d4ac992ab4f",
"version": 1,
"meta": {
"tenantTagId": 33,
"tenant": "engie-vertuoz",
"tag": "test2",
"created": "2019-02-14T15:03:56.000Z",
"createdBy": "aleksandar.bakic@c3iot.com",
"updated": "2019-02-14T15:03:56.000Z",
"updatedBy": "aleksandar.bakic@c3iot.com",
"timestamp": "2019-02-14T15:03:56.000Z"
}
},
{
"type": "PointMeasurementReportHeader",
"id": "root",
"version": 1,
"meta": {
"tenantTagId": 33,
"tenant": "engie-vertuoz",
"tag": "test2",
"created": "2019-02-14T15:03:56.000Z",
"createdBy": "aleksandar.bakic@c3iot.com",
"updated": "2019-02-14T15:03:56.000Z",
"updatedBy": "aleksandar.bakic@c3iot.com",
"timestamp": "2019-02-14T15:03:56.000Z"
}
}
]"
EDIT: I found the root cause: I used order:'descending(evalAt)'
when fetch
ing the object to become prev
, where evalAt
is a timestamp that did not exist on the root
object. But the fetch
returned root
as the latest, so the second time the create
was trying to bind prev
to root
again (although not to that random-id object). After setting evalAt
of the root
to ‘1970-01-01’, it works with @db(unique=['prev'])
.