在seeds.rb中,我的消息模型只有一条记录:
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'
...邮件模型中的电子邮件确实是必需的。但是,当我重设数据库时,我看不出这条记录怎么会无效。电子邮件不需要是唯一的,那么为什么种子命令会产生这样的错误呢?
消息的模型文件:
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已经是db的种子(或者至少在这个命令之后db不是空的)。不过,这也解释不了我为什么不能用
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' ...
发布于 2015-04-17 17:28:41
从模型文件中删除attr_accessor :name, :email, :content解决了这个问题。不知道为什么。我添加了这一行,因为它包含在教程中:http://matharvard.ca/posts/2014/jan/11/contact-form-in-rails-4/,但也没有这一行,消息表单似乎仍然有效。
https://stackoverflow.com/questions/29702533
复制相似问题