select()...one() sometimes returns undefined even though the record exists
I am trying to build upsert functionality for a vertex. My code looks like this:
const existingBuyer = await config.db.select().from('Buyer').where({ id: rawBuyer.id }).one();
if (_.isUndefined(existingBuyer)) {
transaction.let(buyerName, (t) => {
t.create('vertex', 'Buyer')
.set(buyer);
}).let(createsName, (t) => {
t.create('edge', 'Creates')
.from(fromName)
.to(toName)
.set(buyerExtractor.extractCreates(rawBuyer, rawTender));
});
} else {
transaction.let(buyerName, (t) => {
t.update('Buyer')
.set(buyer)
.where({ id: buyer.id })
.return('AFTER');
});
}
The problem is that sometimes existingBuyer is undefined even though the buyer actually exists. Since I have a unique index on id when the transaction is committed with create instead of update I get:
Cannot index record #133:197: found duplicated key '6181c270-0b87-4a59-bfa8-669cc21f346e' in index 'Actor.id' previously assigned to the record #136:135
(Actor is the base class of Buyer)
When I run the query config.db.select().from('Buyer').where({ id: '6181c270-0b87-4a59-bfa8-669cc21f346e'' }).one(); from node console the buyer is returned but when I run it on the transaction.db that just failed I get:
transaction.db.select().from('Buyer').where({ id: '6181c270-0b87-4a59-bfa8-669cc21f346e'}).one().then((b) => console.log(b));
Promise {_bitField: 2097152, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _promise0: undefined, _receiver0: undefined, …}
_bitField:2097152
_boundTo:child {db: ODatabase, _state: Object}
_state:Object {params: Object, paramIndex: 1, select: Array(1), …}
db:ODatabase {transport: BinaryTransport, logger: Object, sessionId: 77, …}
__proto__:Statement {transform: , column: , defaults: , …}
_fulfillmentHandler0:undefined
_promise0:undefined
_receiver0:undefined
_rejectionHandler0:undefined
__proto__:Object {_promiseCreated: , _pushContext: , _popContext: , …}
Also, this doesn't happen for all buyers. Some of them are retrieved and get updated as expected.
I'm using Node.js 8.0.0, OrientDB 2.2.26 and Orientjs:
┬ [email protected]
├── [email protected] deduped
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └─┬ [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │ └── [email protected] deduped
│ └── [email protected]
├── [email protected] deduped
└─┬ [email protected]
└── [email protected]
You can find the entire script in tenders-exposed/elvis-backend-node@e4bd7cb
I also noticed that this issue occurs only the first time I run the script, when there are both creates and updates. If I run it again after all the Buyers have been created and only updates execute they are all retrieved by one() and updated.
Hi @georgiana-b
do you have the instructions for the script in order to reproduce this?
Thanks
@maggiolo00 Just create a graph db, fill in its details in .env, the do npm run migrate and then run node scripts/import_data.js path/to/data/sample/file.json.
I sent you a data sample over email so that you give it a try.
Thank you for looking into this so swiftly!
Thanks @georgiana-b i will give it a try