Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 - How can I add a CHECK .. IN constraint

Tags:

doctrine-orm

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

like image 280
Maarten Avatar asked Jan 24 '12 00:01

Maarten


People also ask

How do I add a check constraint to a column in mysql?

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.

What is constraints define check constraints with example?

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.


1 Answers

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.

like image 192
Ocramius Avatar answered Sep 30 '22 18:09

Ocramius