-
Notifications
You must be signed in to change notification settings - Fork 892
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
PostgreSQL UNIQUE constraint as table constraints #1026
Comments
Working with Indexes covers making them unique. |
I just finished researching this. In Postgres, currently, adding a unique index will enforce uniqueness. In fact that's how unique constraints are actually implemented behind the scenes. However, in 9.4 docs there was this cautionary note:
This note does not appear in 9.5 and 9.6 docs, but neither is there guidance on how we should interpret its disappearance. Another wrinkle is the question of whether columns with unique indexes (but not unique constraints) can be referenced by foreign key constraints. Some people read the docs as saying they cannot:
But, I have tested this with success. Even with my working tests and the disappearance of the warning note, it seems best to create unique constraints when a constraint is your intent. Not only does that make your intent more obvious to other developers, but it sends a clearer signal to Postgres as well—whether or not Postgres reacts any differently at the present time. So this seems to me to be a feature worthy of implementation discussion for Phinx. |
This affects other database adapters as well, such as SQLite. While functionally identical as well, constraints are treated differently (different DDL syntax as well). |
Are you able to provide a patch with your suggested changes as PR? |
Is there any support for this:
http://www.w3resource.com/PostgreSQL/unique.php
CREATE TABLE orders(
ord_no integer,
ord_date date,
item_name character(35),
item_grade character(1),
ord_qty numeric,
ord_amount numeric,
UNIQUE (ord_no)
);
this don't do what i expected:
->addIndex(array("ord_no"), array('unique' => true))
The text was updated successfully, but these errors were encountered: