开发数据库不重置(不为空)
Development database doesn't reset (not null)
在 seeds.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。但是当我重置数据库时,我仍然看不出这条记录怎么会无效。而且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: 我发现 运行ning 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' ...
从模型文件中删除 attr_accessor :name, :email, :content
解决了这个问题。真的不知道为什么。我添加了这一行,因为它包含在教程中:http://matharvard.ca/posts/2014/jan/11/contact-form-in-rails-4/ 但如果没有该行,消息表单似乎仍然有效。
在 seeds.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。但是当我重置数据库时,我仍然看不出这条记录怎么会无效。而且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: 我发现 运行ning 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' ...
从模型文件中删除 attr_accessor :name, :email, :content
解决了这个问题。真的不知道为什么。我添加了这一行,因为它包含在教程中:http://matharvard.ca/posts/2014/jan/11/contact-form-in-rails-4/ 但如果没有该行,消息表单似乎仍然有效。