开发数据库没有重置(非空)

时间:2015-04-17 14:50:03

标签: ruby-on-rails ruby database ruby-on-rails-4 seeding

在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'
...

确实需要在消息模型中使用电子邮件。但是当我重置数据库时,我也不知道这条记录是如何无效的。并且电子邮件不需要是唯一的,那么为什么种子命令会出现这样的错误?

消息的模型文件:

  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' ...

1 个答案:

答案 0 :(得分:0)

从模型文件中删除attr_accessor :name, :email, :content解决了这个问题。不知道为什么。我添加了这一行,因为它包含在教程中:http://matharvard.ca/posts/2014/jan/11/contact-form-in-rails-4/但是如果没有该行,消息表单似乎仍然有效。