How can I add a CHECK .. IN constraint, like in the code below, in Doctrine 2?
CREATE TABLE table_name (
colum_name VARCHAR(1)
CHECK (column_name IN ('A','B','C'))
);
-- edit: I use annotations to define my entities
The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
A check constraint is a rule that specifies the values that are allowed in one or more columns of every row of a base table. For example, you can define a check constraint to ensure that all values in a column that contains ages are positive numbers.
This is not supported by the ORM itself. You can define custom DDL to be used for those columns through your metadata driver. For instance, in the AnnotationDriver
you can use /** @Column(type="string", columnDefinition="VARCHAR(1) CHECK (column_name IN ('A','B','C'))") */
as defined in the Annotations Reference.
I would avoid it anyway and keep those checks on application level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With