rake db:seed 验证失败
Validation failed on rake db:seed
我正在学习 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"
尝试使用:
如果案例已经存在,它将解决它。
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
您的 Gemfile 中是否同时安装了 faker 和 populator?这很可能是问题的一部分。确保你有 运行:
gem install populator #From the command line
并将其包含在您的 Gemfile 中:
gem 'populator'
这是 Git 回购 https://github.com/ryanb/populator/tree/master
的 link
这里也有很棒的文章:http://sudharti.github.io/articles/using-faker-and-populator-rails/
I'm doing chapter 12 of hartle's tutorial. When I ran bundle exec rake
db:seed I got this error:
ActiveRecord::RecordInvalid: Validation failed: Email has already been
taken
当您 运行 rake db:reset
时,它会为您播种数据库。当您然后 运行 rake db:seed
时,将抛出异常,因为您在 seeds.rb
文件中使用 create!
。与 create
不同,create!
在验证失败时引发异常。
您可以通过 运行ning rake db:reset
进行检查,然后使用 rails console
检查您的数据库条目。
您可以采取一些措施来防止这种情况发生,但是当您的数据已经存在时,您为什么要这样做呢?
When I run rake db:populate it gives:
Don't know how to build task 'db:populate'
除非您自己定义,否则不会存在名为db:populate
.
的raketask
你的问题是 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,因此您可以将 User 创建的内容包装在 unless 语句中,例如:
unless User.find_by( email: "example@railstutorial.org" )
# create all 100 users
end
如果您的生产站点仍在使用种子数据(例如 SiteSetting table),这将证明特别有价值;您需要确保数据进入您的生产数据库,但您将创建重复记录(或错误)运行再次使用种子而不丢弃。
作为您问题答案的附加参考,请参阅 this one 的选定答案。
我希望这提供了您需要的所有信息!
我正在学习 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"
尝试使用:
如果案例已经存在,它将解决它。
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
您的 Gemfile 中是否同时安装了 faker 和 populator?这很可能是问题的一部分。确保你有 运行:
gem install populator #From the command line
并将其包含在您的 Gemfile 中:
gem 'populator'
这是 Git 回购 https://github.com/ryanb/populator/tree/master
的 link这里也有很棒的文章:http://sudharti.github.io/articles/using-faker-and-populator-rails/
I'm doing chapter 12 of hartle's tutorial. When I ran bundle exec rake db:seed I got this error:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
当您 运行 rake db:reset
时,它会为您播种数据库。当您然后 运行 rake db:seed
时,将抛出异常,因为您在 seeds.rb
文件中使用 create!
。与 create
不同,create!
在验证失败时引发异常。
您可以通过 运行ning rake db:reset
进行检查,然后使用 rails console
检查您的数据库条目。
您可以采取一些措施来防止这种情况发生,但是当您的数据已经存在时,您为什么要这样做呢?
When I run rake db:populate it gives:
Don't know how to build task 'db:populate'
除非您自己定义,否则不会存在名为db:populate
.
你的问题是 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,因此您可以将 User 创建的内容包装在 unless 语句中,例如:
unless User.find_by( email: "example@railstutorial.org" )
# create all 100 users
end
如果您的生产站点仍在使用种子数据(例如 SiteSetting table),这将证明特别有价值;您需要确保数据进入您的生产数据库,但您将创建重复记录(或错误)运行再次使用种子而不丢弃。
作为您问题答案的附加参考,请参阅 this one 的选定答案。
我希望这提供了您需要的所有信息!