kysely icon indicating copy to clipboard operation
kysely copied to clipboard

Allow `DEFERRABLE` or `modifyEnd/modifyStart` to constraints

Open rubenferreira97 opened this issue 3 years ago • 10 comments

Currently we can't set a constraint (Unique, FK, etc...) as [DEFERRABLE | NOT DEFERRABLE] [INITIALLY DEFERRED | INITIALLY IMMEDIATE] on ALTER TABLE nor CREATE TABLE.

Example:

ALTER TABLE tablename
   DROP CONSTRAINT constraintname,
   ADD  CONSTRAINT constraintname UNIQUE(columnname) DEFERRABLE;
db.schema.alterTable('tablename').dropConstraint('constraintname').execute();
db.schema.alterTable('tablename').addUniqueConstraint('constraintname', ['columnname']).execute(); // No DEFERRABLE nor .modifyEnd 

rubenferreira97 avatar Nov 11 '22 10:11 rubenferreira97

We should add deferrable() and initiallyDeferred() methods. I don't really understand when initially immediate is useful since that's the default. But it can be added too I guess.

This applies to all constraints, including the primary key.

koskimas avatar Nov 12 '22 21:11 koskimas

@koskimas I think initially immediate makes sense if a constraint can be changed (not all dbs allow this). An example would be a constraint created with initially deferred and now we want to change it to initially immediate with ALTER CONSTRAINT. As far as I know initially here means the default transaction "deferred mode". If we don't change it (in Postgres via SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }) it will mantain the value declared after the initially keyword.

For the same reason I think if initially immediate is added to Kysely NOT DEFERRABLE/notDeferrable() should also be taken to consideration. We could change a constraint that was DEFERRABLE to NOT DEFERRABLE. Or maybe ignore that a constraint can be changed at all and don't implement those.

rubenferreira97 avatar Nov 15 '22 12:11 rubenferreira97

@igalklebanov @koskimas can you assign me to this issue?

Thanks 😎 🙏

naorpeled avatar Nov 26 '22 01:11 naorpeled

Sorry guys, been super swamped with stuff to do, it's better if someone else will take this 🙏

naorpeled avatar Mar 27 '23 22:03 naorpeled

Hi @igalklebanov @koskimas I'd love to take this on if its okay. Let me know.

Thanks.

anirudh1713 avatar Apr 06 '23 18:04 anirudh1713

It's yours. Have fun! 🙂

igalklebanov avatar Apr 06 '23 19:04 igalklebanov

Hey all! I apologize for the delay here 🙏, I'm unassigning this for now as I've been busy and will be busy till mid August due to some unexpected things (In good way).

anirudh1713 avatar Jul 10 '23 09:07 anirudh1713

Hi all, I'm on this issue.

viraxslot avatar Nov 20 '23 16:11 viraxslot

@koskimas, Hi, according to the PostgreSQL documentation, DEFERRABLE is applicable to UNIQUE, PRIMARY KEY, EXCLUDE, and REFERENCES constraints only. Therefore, in kysely we cannot use it for the CheckConstraintNode. It seems that creating a separate DeferrableConstraintNode interface is the wrong way of thinking, right? So I just need to duplicate the logic in PrimaryKeyConstraintNode, UniqueConstraintNode and ForeignKeyConstraintNode to avoid future code limitations. Am I correct?

viraxslot avatar Nov 20 '23 18:11 viraxslot

Is there a way to hack around this in the meanwhile using raw sql within a transaction?

JDvorak avatar Jan 27 '24 02:01 JDvorak