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

后端 未结 4 1891
情话喂你
情话喂你 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:11

    The problem was that I had users in the database with the same email before the migration.

    db:reset solved everything

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-13 10:23

    if you can't reset:db you should try to delete it by hand go to db folder and delete the "development.sqlite3" and the "test.sqlite3" files then run rails db:migrate and bin/rails db:migrate RAILS_ENV=TEST

    worked for me, anyway...

    0 讨论(0)
  • 2021-02-13 10:32

    Just use rake db:test:prepare and it will copy the dev database to test so you don't need to run the migrations.

    0 讨论(0)
提交回复
热议问题