lux icon indicating copy to clipboard operation
lux copied to clipboard

fix: await PK on create before updating relationships

Open nickschot opened this issue 6 years ago • 9 comments

In the case of a belongsTo <-> hasOne relationship Lux attempts to update/overwrite the belongsTo FK. For new records (CREATE) however, this has the side effect of (at least with postgres) not working correctly. It attempts to run the query within the CREATE transaction resulting in a "nextval(...)" in the query instead of the (currently nonexistent) primary key.

~~NOTE: side-effect is that duplicate ID's might occur when DB does not have a UNIQUE constraint on the FK.~~

This PR has been updated with code to restructure the create method of the model so it await's the insert query before updating the relationships. This way we are sure to have a valid PK (instead of the nextval stuff).

NOTE: The only downside of this approach is that the relationships are not set on validation. It might be possible to set belongsTo relationships before validation, but we must not use the created query yet as that will use an invalid PK.

nickschot avatar Jan 03 '19 10:01 nickschot

Codecov Report

Merging #737 into stable will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           stable     #737   +/-   ##
=======================================
  Coverage   92.41%   92.41%           
=======================================
  Files         181      181           
  Lines        2018     2018           
=======================================
  Hits         1865     1865           
  Misses        153      153
Impacted Files Coverage Δ
...database/relationship/utils/update-relationship.js 77.41% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 812555a...0b77eea. Read the comment docs.

codecov[bot] avatar Jan 03 '19 10:01 codecov[bot]

Codecov Report

Merging #737 into stable will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           stable     #737   +/-   ##
=======================================
  Coverage   92.41%   92.41%           
=======================================
  Files         181      181           
  Lines        2018     2018           
=======================================
  Hits         1865     1865           
  Misses        153      153
Impacted Files Coverage Δ
...database/relationship/utils/update-relationship.js 77.41% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 812555a...0b77eea. Read the comment docs.

codecov[bot] avatar Jan 03 '19 10:01 codecov[bot]

Codecov Report

Merging #737 into stable will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           stable     #737   +/-   ##
=======================================
  Coverage   92.41%   92.41%           
=======================================
  Files         181      181           
  Lines        2018     2018           
=======================================
  Hits         1865     1865           
  Misses        153      153
Impacted Files Coverage Δ
...database/relationship/utils/update-relationship.js 77.41% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 812555a...c39cdd7. Read the comment docs.

codecov[bot] avatar Jan 03 '19 10:01 codecov[bot]

Codecov Report

Merging #737 into stable will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           stable     #737   +/-   ##
=======================================
  Coverage   92.41%   92.41%           
=======================================
  Files         181      181           
  Lines        2018     2018           
=======================================
  Hits         1865     1865           
  Misses        153      153
Impacted Files Coverage Δ
...database/relationship/utils/update-relationship.js 77.41% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 812555a...0b77eea. Read the comment docs.

codecov[bot] avatar Jan 03 '19 10:01 codecov[bot]

I am now thinking this error is caused by something (presumably lux) parsing the nextVal function into a string causing Postgres to error.

nickschot avatar Jan 08 '19 13:01 nickschot

This problem also exists with the update hasOne relationship.

nickschot avatar Jan 09 '19 11:01 nickschot

~~See #739 , the "fix" proposed in this PR is not really desirable.~~

nickschot avatar Jan 10 '19 13:01 nickschot

PR has been updated with a new and much better fix.

nickschot avatar Jan 23 '19 11:01 nickschot

Any progress on evaluating/merging this fix?

mdconaway avatar Aug 09 '19 23:08 mdconaway