Skip to content

Conversation

raphaelgibson
Copy link
Contributor

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

@hvonbargen
Copy link

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.

@christophelec
Copy link

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.

@nuno-andre
Copy link

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

@raphaelgibson raphaelgibson force-pushed the feature/unique-constraint branch from dd9dc12 to 4a29185 Compare February 7, 2022 20:33
@honglei
Copy link

honglei commented Feb 27, 2022

Any updates?

@raphaelgibson raphaelgibson force-pushed the feature/unique-constraint branch from 4a29185 to 66156d4 Compare February 27, 2022 04:57
@raphaelgibson
Copy link
Contributor Author

Any updates?

Waiting the merge.

@raphaelgibson raphaelgibson force-pushed the feature/unique-constraint branch from 66156d4 to 2b39e32 Compare June 10, 2022 04:13
@tiangolo tiangolo changed the title feat: add unique constraint param to Field function ✨ Allow setting unique in Field() for a column Aug 27, 2022
@tiangolo
Copy link
Member

tiangolo commented Aug 27, 2022

Awesome! Thanks for writing thorough tests @raphaelgibson! 🚀 🙇 🍰

This will be available in SQLModel 0.0.7, released in the next hours. 🎉

@github-actions
Copy link
Contributor

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

@codecov
Copy link

codecov bot commented Aug 27, 2022

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%) ⬆️
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.

@tiangolo tiangolo merged commit 42b0e6e into fastapi:main Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants