Add check constraints to a table
I couldn't find a way to execute something like
alter table table.column add constraint column_length check (length(column) >= 6);
Currently it can only be done by executing a raw query
you can make use the alter query "add constraint " and specify the criteria of particular constraint to a column.
Should addColumn include a constraint or check option? It feels a bit out of place adding a custom, raw ALTER TABLE to "finish" a new column in a whole table written with Phinx.
However, given the age of this issue, I guess checks and constraints are a rare use case? haha
Me too looked for method for adding check constraint while creating a table. Done as follows:
if ($this->isMigratingUp()) {
$this->execute('ALTER TABLE emails
ADD CONSTRAINT chk_email_primary
CHECK (main IN (1,0))');
}
But as man rightly mentioned above - "It feels a bit out of place"
Yep, would be cool :)
->addColumn('type', 'text', [ 'null' => false, 'check' => "type = 'text' OR type = 'integer'" ])
I'm using SQLite and doing this is not possible using the Table class, I must use raw queries.