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
[WIP] Check constraints #2364
[WIP] Check constraints #2364
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this has some bugs which were commented to code rows.
Also this would need implementation for all the supported dialects, because if this is implemented only for 1 dialect it is the same just to use knex.schema.raw
(there was a time when I was accepting single db implementations for some features, it was a bad decision).
Lastly tests and documentation is completely missing.
@@ -90,6 +90,11 @@ TableCompiler_PG.prototype.comment = function(comment) { | |||
this.pushQuery(`comment on table ${this.tableName()} is '${this.single.comment}'`); | |||
}; | |||
|
|||
TableCompiler_PG.prototype.check = function(logic) { | |||
const name = this.formatter.wrap(`${this.tableNameRaw}_check`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this naming convention limits number of check constraints per table to be 1.
@@ -90,6 +90,11 @@ TableCompiler_PG.prototype.comment = function(comment) { | |||
this.pushQuery(`comment on table ${this.tableName()} is '${this.single.comment}'`); | |||
}; | |||
|
|||
TableCompiler_PG.prototype.check = function(logic) { | |||
const name = this.formatter.wrap(`${this.tableNameRaw}_check`); | |||
this.pushQuery(`alter table ${this.tableName()} add constraint ${name} check (${logic})`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs to support raw() syntax, or otherwise identifier/value bindings cannot be handled properly across the dialects
And there was some indentation error which caused CI to fail :) |
@elhigu The following all fail with assorted errors being thrown in await knex.raw('alter table _test3 add constraint ? check (length(name) >= 3)', ['name_check'])
// or
await knex.raw('alter table _test3 add constraint name_check check (length(?) >= 3)', ['name'])
// or
await knex.raw('alter table _test3 add constraint name_check check (length(?) >= ?)', ['name', 3])
// or
await knex.raw('alter table _test3 add constraint ? check (length(?) >= ?)', ['name_check', 'name', 3]) |
Probably all of those examples already throw an error because PG does not use e.g. raw('?? = ?', ['colName', 'foo']) will pass string
to the driver. But it is necessary that knex can do its wrapping to make quotes right (and make use of custom identifier wrapping if it is set in knex configuration). |
A MySQL implementation should also be included. I'm aware that MySQL parses I have one question though, is it currently possible to implement the await knex.schema.createTable('_test', (t) => {
t.increments()
t.date('created')
t.date('updated')
t.raw('check updated >= created')
}) |
@richraid21 Are you still interested in finishing this? |
Superseded by #4874 |
Adds check constraint methods to both column and table level APIs
See Postgres Check Constraint Docs
Use case:
TODO
Determine name scheme for supporting multiple table-level constraints
Determine API for named and default-named constraints
Postgres Implementation https://www.postgresql.org/docs/9.4/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
Mysql ImplementationUnsupported See "Check Constraints" https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.htmlMaria Implementation
SQL Server Implementation https://www.w3schools.com/sql/sql_check.asp
Oracle Implementation https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/create-table-check-constraints.html
Tests for all above
Documentation
Still need to write tests and documentation