sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

feat: add unique constraint param to Field function

Open raphaelgibson opened this issue 3 years ago • 5 comments

Add unique constraint param to Field function to create unique columns. I have added tests for these modifications.

raphaelgibson avatar Sep 07 '21 03:09 raphaelgibson

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.

hvonbargen avatar Sep 26 '21 02:09 hvonbargen

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.

christophelec avatar Oct 02 '21 21:10 christophelec

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

nuno-andre avatar Dec 15 '21 21:12 nuno-andre

Any updates?

honglei avatar Feb 27 '22 04:02 honglei

Any updates?

Waiting the merge.

raphaelgibson avatar Feb 27 '22 04:02 raphaelgibson

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:

tiangolo avatar Aug 27 '22 23:08 tiangolo

📝 Docs preview for commit c2c66701cbca3ed5ad787e408d0389dd11b6b98d at: https://630aacc9a5277f58b9062b3e--sqlmodel.netlify.app

github-actions[bot] avatar Aug 27 '22 23:08 github-actions[bot]

Codecov Report

Merging #83 (9f80059) into main (1ca2880) will increase coverage by 0.02%. The diff coverage is 100.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.

codecov[bot] avatar Aug 27 '22 23:08 codecov[bot]