lux
lux copied to clipboard
[BUG] PK not set properly when creating a record w/ a hasOne relationship
- Platform: macOS 10.14
- Database: postgres 9.6
- Lux Version: 1.2.3
- Node Version: 11
Having two models with a hasOne <-> belongsTo relationship, when creating the model which has the hasOne side two queries are run:
- The main INSERT query (which has a
returning 'id'statement) - An UPDATE query which sets the relationship on the record with the belongsTo side (Created by: https://github.com/postlight/lux/blob/master/src/packages/database/model/index.js#L1229 )
Due to the way the queries are built (and run), the PK is not correctly set on the UPDATE query resulting in a 'nextval(''transactions_id_seq''::regclass)' in Postgres. The newly created PK is only set on the model after the transaction with the queries has completed. See: https://github.com/postlight/lux/blob/master/src/packages/database/model/index.js#L1243 through line 1246.
@zacharygolba it would be great if you could comment on this. I have been debugging lux for a while now, but haven't fully got an idea of how to fix this without fully overhauling the query builder/runner.
PS. https://github.com/postlight/lux/pull/737 is caused by this too.
Fix is in #737