【发布时间】:2015-04-17 14:50:02
【问题描述】:
在 seed.rb 中,我的 Messages 模型只有一条记录:
Message.create!(email: "example@example.com",
name: "Example User",
content: "This is my message")
如果我运行rake db:seed,我会收到错误消息:
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Username has already been taken
/usr/local/rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
...
如果我运行 bundle exec rake db:reset 我会收到错误:
-- initialize_schema_migrations_table()
-> 0.0734s
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: messages.name: INSERT INTO "messages" ("created_at", "updated_at") VALUES (?, ?)
/usr/local/rvm/gems/ruby-2.1.5/gems/sqlite3-1.3.10/lib/sqlite3/statement.rb:108:in `step'
...
Message 模型中的电子邮件确实是必需的。但是当我重置数据库时,我看不出这条记录是如何无效的。而且email不需要是唯一的,为什么seed命令会报这样的错误呢?
消息的模型文件:
attr_accessor :name, :email, :content
validates :name, presence: true,
length: { maximum: 255 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :content, presence: true,
length: { maximum: 600 }
消息迁移文件:
def change
create_table :messages do |t|
t.string :name, null: false, limit: 255
t.string :email, null: false, limit: 255
t.string :content, null: false, limit: 600
t.timestamps null: false
end
end
我不确定是什么导致了这个问题...
更新:我迷路了,即使没有 Message.create,我现在在播种时也会收到错误消息!种子文件中的行。错误显示rake aborted! ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Username has already been taken。但是自从我第一次运行bundle exec rake db:reset 以来,这怎么可能呢?这不会从数据库中删除所有数据吗?根据定义,这是否意味着它们已经不能被使用?
我的种子文件是:
User.create!(fullname: "Example User",
username: "fakename0",
email: "example@railstutorial.org",
admin: true,
activated: true,
activated_at: Time.zone.now,
password: "foobar",
password_confirmation: "foobar")
User.create!(fullname: "Example User 2",
username: "fawwkename0",
email: "exaaample@railstutorial.org",
admin: false,
activated: true,
activated_at: Time.zone.now,
organization: "organization",
password: "foobar",
password_confirmation: "foobar")
99.times do |n|
username = "fakename#{n+1}"
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(username: username,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
更新 2: 我发现运行 rake db:reset 已经为数据库播种(或者至少在此命令之后数据库不是空的)。但这并不能解释为什么我不能用它播种
Message.create!(email: "example@example.com",
name: "Example User",
content: "This is my message")
播种此数据会产生错误:rake aborted!
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: messages.name: INSERT INTO "messages" ("created_at", "updated_at") VALUES (?, ?) /usr/local/rvm/gems/ruby-2.1.5/gems/sqlite3-1.3.10/lib/sqlite3/statement.rb:108:in 'step' ...
【问题讨论】:
-
顺便说一句,如果我从种子文件中删除记录,一切都会顺利进行,并且重置和种子命令不会产生任何错误。
-
只是小费。不要使用正则表达式(按格式样式)验证电子邮件地址。简单检查
@就够了。 -
我发现
rake db:reset已经为数据库播种,这部分导致了问题。
标签: ruby-on-rails ruby database ruby-on-rails-4 seeding