You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
(Postgres) Unique Property Error, Can it be formatted as a Validation Error? #653
I'm new to Sequelize, so pardon any incorrect terminology/assumptions.
I have a unique property {unique: true} and if data is submitted which violates that, Sequelize returns a real Error. Eg:
Error: duplicate key value violates unique constraint "Users_email_key"
at Connection.connection.on.q (/home/foo/node_modules/pg/lib/native/index.js:188:17)
at Connection.EventEmitter.emit (events.js:96:17)
Now, i understand this is coming from the pg library, but is there any way this could be returned as a validation error? That is to say, Sequelize currently returns validation errors as an object, with properties as keys and within each is a list of failures. It would be nice if there was a way to make that Unique error be returned in the Sequelize validation error format. There may be other non-horrible errors that would also be better as a sequelize validation error as well.
There's actually quite a few things I want to do with the primaryKeys and uniques.... pinging everyone @sdepold@mickhansen@mickhansen for your feedback/input (and I'll get to work afterwards)...
Right now we simply have the following:
primaryKey: true
unique: true
This is rather limited, to just associating one unique with one column, as well as associating one column with one primary key, so I'd like to change the semantics a bit (NOTE: We will STILL support true as-is, it's just a quick way of mapping 1:1 column:key association).
Proposal
column: {primaryKey: 'This primary key already exists!'// this will allow us to set a custom error message if we violate the constraint},column2: {unique: {group: 'clusterIndex1',msg: 'Sorry man, but we\'re pretty liberal with our uniques we allow both column2 and column3 to be unique!'}},column3: {unique: 'clusterIndex1'}
So a little bit of an explanation:
primaryKey only needs to be a string (assuming we don't want to give special constraint names [which would actually be a nice feature for us postgres users]) since you just need a hasOwnProperty + not false boolean value to say "OK this is a PK" Now, for postgres users and special names we can do...
The msg is following the validations tradition, and feel free to chime in on wanting to make name into group (or make unique's group into name doesn't matter to me tbh but ideally we want both to be the same.
unique same thing really as the PK
Now the interesting part is... once we declare a primaryKey with an error message, that error message will be applied to the other primary keys as well (since in reality, it's all one key). So for the other columns you would only need primaryKey: true in order to register that column as a PK.
For the unique.. it's even more interesting once we declare a group/name, since any column after that will literally only need the group's name (as a string value), you can still declare true (but this will make the column a separate unique index from the previously declared unique column)
In addition to primaryKey and unique, I'd also like to make index: ... (same concept as unique, except for normal indexes).
Why is this important?
Declaring the indexes on the columns helps keep Sequelize compact and easy to manage (no need for a another key in that third argument on .define()).
Sequelize can re-organize your where parameters in the correct order for your DBMS to utilize the index
Sequelize can also opt to force use an index for select queries, this can be an additional parameter in the find/all/etc. argument (something like {force: ['indexName']}
Activity
durango commentedon Aug 9, 2013
There's actually quite a few things I want to do with the primaryKeys and uniques.... pinging everyone @sdepold @mickhansen @mickhansen for your feedback/input (and I'll get to work afterwards)...
Right now we simply have the following:
This is rather limited, to just associating one unique with one column, as well as associating one column with one primary key, so I'd like to change the semantics a bit (NOTE: We will STILL support true as-is, it's just a quick way of mapping 1:1 column:key association).
Proposal
So a little bit of an explanation:
primaryKey only needs to be a string (assuming we don't want to give special constraint names [which would actually be a nice feature for us postgres users]) since you just need a hasOwnProperty + not false boolean value to say "OK this is a PK" Now, for postgres users and special names we can do...
The msg is following the validations tradition, and feel free to chime in on wanting to make name into group (or make unique's group into name doesn't matter to me tbh but ideally we want both to be the same.
unique same thing really as the PK
Now the interesting part is... once we declare a primaryKey with an error message, that error message will be applied to the other primary keys as well (since in reality, it's all one key). So for the other columns you would only need
primaryKey: truein order to register that column as a PK.For the unique.. it's even more interesting once we declare a group/name, since any column after that will literally only need the group's name (as a string value), you can still declare true (but this will make the column a separate unique index from the previously declared unique column)
In addition to primaryKey and unique, I'd also like to make
index: ...(same concept as unique, except for normal indexes).Why is this important?