Skip to content

[BUG] PK not set properly when creating a record w/ a hasOne relationship #739

@nickschot

Description

@nickschot
  • 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:

  1. The main INSERT query (which has a returning 'id' statement)
  2. 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. #737 is caused by this too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions