phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Add check constraints to a table

Open vitoacme opened this issue 7 years ago • 5 comments

I couldn't find a way to execute something like

alter table table.column add constraint column_length check (length(column) >= 6);

vitoacme avatar Nov 08 '18 20:11 vitoacme

Currently it can only be done by executing a raw query

lorenzo avatar Nov 14 '18 13:11 lorenzo

you can make use the alter query "add constraint " and specify the criteria of particular constraint to a column.

dinesh419 avatar Apr 09 '19 16:04 dinesh419

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

igorsantos07 avatar Jun 05 '21 16:06 igorsantos07

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"

solventt avatar Aug 05 '21 22:08 solventt

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.

Rakjlou avatar Oct 07 '23 00:10 Rakjlou