Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique constraint on multiple fields #78

Open
petersimun opened this issue Jun 15, 2016 · 5 comments
Open

Unique constraint on multiple fields #78

petersimun opened this issue Jun 15, 2016 · 5 comments

Comments

@petersimun
Copy link

i know it's possible to define in model that field is unique. Is it possible to do so over multiple columns/fields?

Thanks
Peter

@scottwrobinson
Copy link
Owner

Let me make sure I'm understanding your request correctly. If you had a model, Person, with unique-constrained fields firstName and lastName, you want to make sure that only one Person document can have a given firstName and lastName? So there couldn't be two {firstName: 'Scott', lastName: 'Robinson'} instances in the DB, correct?

@petersimun
Copy link
Author

exactly!

@michaeljota
Copy link

You can do it straightforward. Just set it in the schema, or the constructor. :)

@petersimun
Copy link
Author

how? can you provide me an example please?

@michaeljota
Copy link

michaeljota commented Jul 21, 2016

It's in the docs. (README)

The default option supports both values and no-argument functions (like Date.now). Currently the supported options/validators are:

type: The value's type (required)
default: The value to be assigned if none is provided (optional)
min: The minimum value a Number can be (optional)
max: The maximum value a Number can be (optional)
choices: A list of possible values (optional)
match: A regex string that should match the value (optional)
validate: A 1-argument function that returns false if the value is invalid (optional)
unique: A boolean value indicating if a 'unique' index should be set (optional)
required: A boolean value indicating if a key value is required (optional)

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

No branches or pull requests

3 participants