sea-query icon indicating copy to clipboard operation
sea-query copied to clipboard

Support Table `CHECK` Constraints

Open billy1624 opened this issue 3 years ago • 4 comments

Discussed in https://github.com/SeaQL/sea-query/discussions/135

Originally posted by forrest-akin August 3, 2021 Currently, there's no way to specify table CHECK constraints in TableCreateStatements. Adding an .extra() method similar to ColumnDef would probably be the easiest way to add support for this.

billy1624 avatar Jul 26 '22 08:07 billy1624

After some digging I found all three databases support CHECK constraint:

  • SQLite: https://www.sqlite.org/lang_createtable.html#check_constraints
  • MySQL: https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
  • PostgreSQL: https://www.postgresql.org/docs/current/ddl-constraints.html

billy1624 avatar Jul 26 '22 08:07 billy1624

I'll open this issue for contributions

billy1624 avatar Jul 26 '22 08:07 billy1624

👋 Hey @billy1624! Happy to claim this if it's okay with you!

nahuakang avatar Aug 12 '22 11:08 nahuakang

wave Hey @billy1624! Happy to claim this if it's okay with you!

Done!

ikrivosheev avatar Aug 12 '22 11:08 ikrivosheev

@nahuakang is there any news with this?

Razican avatar Oct 20 '22 15:10 Razican

@Razican Sorry I haven't had the time to got around to this issue. Are you interested in taking it? If so, please do :) I probably will only come back to this in 2-3 weeks earliest.

nahuakang avatar Oct 20 '22 15:10 nahuakang

Id be happy to give it a go :)

LemarAb avatar Oct 30 '22 23:10 LemarAb

@LemarAb hello! Any updates?)

ikrivosheev avatar Nov 25 '22 19:11 ikrivosheev

@ikrivosheev I made some progress but need some time to deeper understand how sea query translates the stmt into SQL. Will report soon. If necessary, can I ask you for help?

LemarAb avatar Nov 25 '22 20:11 LemarAb

@ikrivosheev I made some progress but need some time to deeper understand how sea query translates the stmt into SQL. Will report soon. If necessary, can I ask you for help?

Take your time! Yes, of course you can)

ikrivosheev avatar Nov 25 '22 20:11 ikrivosheev

Just to clarify: CHECK constraints on Columns are already supported through the extra() method. We want to add CHECK constraints for the whole table, right? As an example for Postgres:

CREATE TABLE products (
    price integer CHECK(product_no > 0),
    discounted_price integer,
    CONSTRAINT valid_discount CHECK (product_no > discounted_price)
);

We want to add support for the second constraint, the first is already supported, correct?

@ikrivosheev

LemarAb avatar Nov 27 '22 14:11 LemarAb

@LemarAb we want to add the ability to add CHECK constraint using SeaQuery, without extra method.

It will great if we can both)

ikrivosheev avatar Nov 28 '22 09:11 ikrivosheev

@ikrivosheev ok, so afaik we need to add check() to ColumnDef and to table/create ?

LemarAb avatar Nov 28 '22 11:11 LemarAb