039. 数据库中存在不符合约束条件的数据ERROR: check constraint "email_must_be_company_email" is violated by some row - cwy007/tips-and-skills GitHub Wiki

1. 迁移时报错

bundle exec rails db:migrate
== 20180110165533 AddEmailConstraintToUsers: migrating ========================
-- execute("\n      ALTER TABLE\n        users \n      ADD CONSTRAINT\n        email_must_be_company_email\n      CHECK ( email ~* '^[^@]+@example\\.com' )\n    ")
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::CheckViolation: ERROR:  check constraint "email_must_be_company_email" is violated by some row
:
      ALTER TABLE
        users
      ADD CONSTRAINT
        email_must_be_company_email
      CHECK ( email ~* '^[^@]+@example\.com' )

2. 解法

将原有不符合条件的数据删除

rails db:drop
rails db:create
rails db:migrate