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
findOrCreate inserts but throws a SequelizeUniqueConstraintError (MySQL) #5134
Comments
Did some more digging. user_id field is set to auto_increment in the database, but wasn't configured that way in the model. Error was gone after updating the model, but I still suggest the idea it should rollback when it throws an Unique Constraint Exception. |
I am facing the same issue. Creation succeds but also gives a SequelizeUniqueConstraintError. |
If it is due to the id field being set to auto increment, setting the field's auto increment property on your model fixes it. Example: |
@okankaya thanks ,you save me ! |
I am confused by this functionality because if the query finds a record under the given parameters shouldn't it return that value and skip the create part? Otherwise this is a findAndCreate method which doesn't make sense. I am using this Facebook OAuth and searching for their FB id so the increment solution mentioned is not available to me. Expected results: Creates row in database with user data when logging into app for the first time, on subsequent login attempts it retrieves this existing data and does not try to create new user instance. Notes: Using Postgres 9.5.4 & Sequelize 3.30.2
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂 |
Hello! |
I am also experiencing the same issue as @nandeshnair: Error msg:
• db (sql server) unique constraint attached |
@charlieargue Hi Karl! Can you please share your findOrCreate method call as well? An example from the official documentation:
|
Model.upsert({ Use upser to prevnt this case |
In the example findOrCreate is inserting to the table when the record doesn't exist, but then throw a SequelizeUniqueConstraintError. I was expecting it to rollback in this case since this is supposed to be a single commit, but it doesn't.
When above code is executed, it executes the following SQL queries:
Logged error message is as follows:
When the above insert statement is executed on the database directly, record is being inserted without an issue.
The text was updated successfully, but these errors were encountered: