Rails Tutorial: SQLite3::ConstraintException: UNIQUE constraint failed: users.email

后端 未结 4 1906
情话喂你
情话喂你 2021-02-13 10:02

I\'m following along with the rails tutorial. I\'m on ch. 6 and I\'m getting a strange error with SQLite3 (for the record, I\'m using sqlite version 1.3.10 and the tutorial uses

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 10:22

    I experienced it for a bit different reason and here is the solution.

    For me the error was

    Minitest::UnexpectedError: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2018-05-09 18:23:59.827561', '2018-05-09 18:23:59.827561', 298486374)
    

    Notice the INTO "users" ("created_at", "updated_at", "id"). It was caused by test/fixtures/users.yml was not filled, so then there was duplicate values in columns which has unique key constraint.

    test/fixtures/users.yml:

    # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
    
    # This model initially had no columns defined. If you add columns to the
    # model remove the '{}' from the fixture names and add the columns immediately
    # below each fixture, per the syntax in the comments below
    #
    one: {}
    # column: value
    #
    two: {}
    # column: value
    

提交回复
热议问题