【问题标题】:Validation failed on rake db:seedrake db:seed 验证失败
【发布时间】:2015-07-11 06:22:42
【问题描述】:

我正在学习 hartle 教程的第 12 章。当我运行bundle exec rake db:seed 时,我得到了这个错误:

ActiveRecord::RecordInvalid: Validation failed: Email has already been taken

我尝试跑步

rake db:reset
rake db:migrate
rake db:test:prepare

最后

rake db:populate 

但他们没有解决问题。当我运行rake db:populate 时,它给出:

Don't know how to build task 'db:populate'

这是我的seeds.rb 文件:

    # Users
User.create!(name:  "Example User",
             email: "example@railstutorial.org",
             password:              "foobar",
             password_confirmation: "foobar",
             admin:     true,
             activated: true,
             activated_at: Time.zone.now)

99.times do |n|
  name  = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name: name,
              email: email,
              password:              password,
              password_confirmation: password,
              activated: true,
              activated_at: Time.zone.now)
end

# Microposts
users = User.order(:created_at).take(6)
50.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.microposts.create!(content: content) }
end

# Following relationships
users = User.all
user  = users.first
following = users[2..50]
followers = users[3..40]
following.each { |followed| user.follow(followed) }
followers.each { |follower| follower.follow(user) }

我想问题可能出在这条线email = "example-#{n+1}@railstutorial.org"

【问题讨论】:

  • 我看不出你的种子文件有问题。您确定数据库正确删除了吗?也许进入 rails c 并检查是否有任何持续存在。或者尝试捕获异常并确定导致问题的记录。

标签: ruby-on-rails ruby railstutorial.org


【解决方案1】:

您的问题是 rake db:reset 不仅删除并重新创建数据库,而且it also migrates and seeds it as well。所以基本上发生的事情是这样的:

rake db:drop
rake db:create
rake db:schema:load # (think of this as running all the migrations you've run before)
rake db:seed # (creates your 100 database users)

然后你运行:

rake db:migrate # (likely unnecessary, but it causes no harm)
rake db:test:prepare # (prepares the test database)
rake db:prepare # (runs the seeds AGAIN and causes your errors)

显然,如果您停止运行 rake db:prepare 命令,您的问题就会消失。但是,为了将来避免这些事情,我强烈建议在您的种子文件中添加一些逻辑。它只是 Ruby,因此您可以将用户创建的内容包装在一个除非语句中,例如:

unless User.find_by( email: "example@railstutorial.org" )
  # create all 100 users
end

如果您有一个仍在使用种子数据的生产站点(例如 SiteSetting 表),这将证明特别有价值;您需要确保数据进入您的生产数据库,但您将创建重复记录(或错误)再次运行种子而不会丢失。

作为您问题答案的附加参考,请参阅this one 的选定答案。

我希望这能提供您需要的所有信息!

【讨论】:

  • 感谢您的回答...我在没有 rake db:prepare 的情况下运行您提到的命令,但错误仍然存​​在...我很困惑,真的不知道该怎么办!
  • 为了清楚起见,您在开始时运行了四个命令 (rake db:reset, rake db:migrate, rake db:test:prepare, rake db:prepare),最后一个命令出错,对吗?如果是这样,就不要运行最后一个。你得到的错误是因为最后一个命令是多余的。
  • 不,我没有运行 'rake db:prepare'
  • 那么当错误现在出现时,您到底在运行什么? rake db:prepare, rake db:reset, rake db:seed, and bundle exec rake db:Seed 都在运行你的种子。使用多个总是会给你这个错误。
  • 啊,我明白了。你是对的,没有问题。哈特尔先生希望你在那篇文章中做的就是将种子放入你的数据库中,而你已经成功地做到了。
【解决方案2】:

我正在学习 hartle 教程的第 12 章。当我运行 bundle exec rake db:seed 我收到了这个错误:

ActiveRecord::RecordInvalid:验证失败:电子邮件已经 拍摄

当您运行rake db:reset 时,它会为您播种数据库。然后当您运行rake db:seed 时,将引发异常,因为您在seeds.rb 文件中使用了create!。与create 不同,create! 在验证失败时引发异常。

您可以通过运行rake db:reset 进行检查,然后使用rails console 检查您的数据库条目。

您可以采取一些措施来防止这种情况发生,但是当您的数据已经存在时,您为什么还要这样做呢?

当我运行 rake db:populate 时,它​​会给出:

不知道如何构建任务 'db:populate'

除非您自己定义,否则没有名为 db:populate 的 rake task

【讨论】:

    【解决方案3】:

    尝试使用:

    如果案例已经存在,它会解决它。

    email = "example-#{rand(100000)}@railstutorial.org"
    

    你也可以看到错误:

    user = User.new(name:  "Example User",
             email: "example@railstutorial.org",
             password:              "foobar",
             password_confirmation: "foobar",
             admin:     true,
             activated: true,
             activated_at: Time.zone.now)
    user.errors
    user.save if user.valid
    

    【讨论】:

    • 同样的事情'验证失败:电子邮件已被占用'
    • 转到控制台并查看User.all.pluck(:email).sort 的输出并查看电子邮件ID 是否存在于example-1@railstutorial.org .....example-100@railstutorial.org
    • 其中一个是最初定义的“example@railstutorial.org”,其他在example-1@railstutorial.org和example-100@railstutorial.org之间
    • 并且您对用户电子邮件进行了验证。所以User.delete_all 然后rake db:seedrake db:drop db:create db:migrate 然后rake db:seed 应该可以工作
    • 我按照你说的删除了所有用户并运行了 rake db:drop rake db:create 、 rake db:migrate 、 rake db:seed ......但是之后当我运行 bundle exec rake db:seed我又看到了:(并且在 User.all.pluck(:email).sort 我可以再次看到所有这些电子邮件!) rake 中止了! ActiveRecord::RecordInvalid:验证失败:电子邮件已被使用
    【解决方案4】:

    您的 Gemfile 中是否同时安装了 faker 和 populator?这很可能是问题的一部分。确保你已经运行:

    gem install populator #From the command line
    

    并将其包含在您的 Gemfile 中:

    gem 'populator'
    

    这里是 Git 存储库https://github.com/ryanb/populator/tree/master的链接

    这里也有很棒的文章:http://sudharti.github.io/articles/using-faker-and-populator-rails/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多