Unhandled rejection SequelizeUniqueConstraintError: Validation error

后端 未结 5 538
花落未央
花落未央 2021-02-01 23:47

I\'m getting this error:

Unhandled rejection SequelizeUniqueConstraintError: Validation error

How can I fix this?

This is my models/use

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 00:20

    I had this issue with my QA database. Sometimes a new record would save to the database, and sometimes it would fail. When performing the same process on my dev workstation it would succeed every time.

    When I caught the error (per @doublesharp's good advice) and printed the full results to the console, it confirmed that a unique constraint as being violated - specifically, the primary key id column, which was set to default to an autoincremented value.

    I had seeded my database with records, and even though the ids of those records were also set to autoincrement, the ids of the 200-some records were scattered between 1 and 2000, but the database's autoincrement sequence was set to start at 1. Usually the next id in sequence was unused, but occasionally it was already occupied, and the database would return this error.

    I used the answer here to reset the sequence to start after the last of my seeded records, and now it works every time.

提交回复
热议问题