sqlmodel
sqlmodel copied to clipboard
feat: add unique constraint param to Field function
Add unique constraint param to Field function to create unique columns. I have added tests for these modifications.
I think that a column is not the right place for a unique constraint. And there can be more than one unique constraints for a table. The natural representation for a unique constraint is a list of columns. And a unique constraint should have a name.
I think a column can perfectly be the right place for a unique constraint, if the unicity constraint is on one column only.
The possibility to create constraints on multiple columns should of course exist as well, but adding a nameless unique constraint to a single column is a pretty common use case.
And it seems people from postgresql agree : https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS.
Hi @christophelec!
I would also set index
to False
if Undefined
(which is the SQLAlchemy's default) so that a UNIQUE CONSTRAINT
clause be included in the column definition. Otherwise, an index with unique=True
will be generated.
https://github.com/sqlalchemy/sqlalchemy/blob/a91df497d8d78292b0b5e7f79656b3f82d7de4f7/lib/sqlalchemy/sql/schema.py#L1448-L1509
Any updates?
Any updates?
Waiting the merge.
Awesome! Thanks for writing thorough tests @raphaelgibson! :rocket: :bow: :cake:
This will be available in SQLModel 0.0.7
, released in the next hours. :tada:
📝 Docs preview for commit c2c66701cbca3ed5ad787e408d0389dd11b6b98d at: https://630aacc9a5277f58b9062b3e--sqlmodel.netlify.app
Codecov Report
Merging #83 (9f80059) into main (1ca2880) will increase coverage by
0.02%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## main #83 +/- ##
==========================================
+ Coverage 97.70% 97.72% +0.02%
==========================================
Files 185 186 +1
Lines 6133 6203 +70
==========================================
+ Hits 5992 6062 +70
Misses 141 141
Impacted Files | Coverage Δ | |
---|---|---|
sqlmodel/main.py | 84.72% <100.00%> (+0.13%) |
:arrow_up: |
tests/test_main.py | 100.00% <100.00%> (ø) |
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.