Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for "DEFEFFERED INITIALLY DEFERRED" in schemas? #1830

Closed
scf4 opened this issue Dec 13, 2016 · 8 comments
Closed

Option for "DEFEFFERED INITIALLY DEFERRED" in schemas? #1830

scf4 opened this issue Dec 13, 2016 · 8 comments

Comments

@scf4
Copy link

scf4 commented Dec 13, 2016

When I do something like:

table.integer('user_id').unsigned().index().references('id').inTable('users');

and then create a reference inside a transaction (a reference to a row created just before it in the same transaction). It fails unless I defer the foreign key with the 'initially deferred' option.

Do I need to alter my schemas manually like:

ALTER TABLE table_name
  ADD CONSTRAINT "constraint_name" FOREIGN KEY ("blah_id")
    REFERENCES "blahs"("id")
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    DEFERRABLE 
    INITIALLY DEFERRED;

Or is there an easier way within Knex?

@derMart
Copy link

derMart commented Dec 14, 2016

+1 have the same problem

@derMart
Copy link

derMart commented Dec 15, 2016

Here is the workaround I use currently:
https://gist.github.com/derMart/87e75fbd1fdca73d6d3cebf561ca3fc6
Automatically alters all foreign key constraints to be deferrable. Of course this doesn't suit all situations. A native knex.js solution would be very nice

@scf4
Copy link
Author

scf4 commented Dec 17, 2016

Thank you @derMart

I'll dig into knex and see if I can create a PR after Christmas if there isn't one by then.

@7sempra
Copy link

7sempra commented Mar 25, 2017

+1, this is a real issue in sqlite where we can't alter constraints after creating them.

@scf4
Copy link
Author

scf4 commented Jun 18, 2017

In the mean time here's a greatly simplified version of what @derMart posted without lodash and all that nesting:
https://gist.github.com/scf4/03780af508218200a590959d8258f61c

Change export default to module.exports = if you don't use babel for imports.

@mlarcher
Copy link

mlarcher commented Oct 5, 2020

Is there any chance to have a Knex native way to set "DEFERRABLE INITIALLY DEFERRED" on a table with foreign key constraints ?
Time flies but this quick feature would still come in handy today...

@RusinovAnton
Copy link

I need this feature too

@elhigu
Copy link
Member

elhigu commented Nov 11, 2020

Closing old issue with low activity. The feature request doesn't meet current criteria of having API proposal etc.

If someone still would like to design this, please open a new feature request.

@elhigu elhigu closed this as completed Nov 11, 2020
@knex knex locked as resolved and limited conversation to collaborators Nov 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants