错误:ActiveRecord::语句无效:Mysql2::错误:字段';全名';不';没有默认值

错误:ActiveRecord::语句无效:Mysql2::错误:字段';全名';不';没有默认值,mysql,ruby-on-rails,unit-testing,activerecord,Mysql,Ruby On Rails,Unit Testing,Activerecord,我在staff表上定义了一列“full_name”,该列也设置为不为null。当我运行下面的测试(patient_test.rb)时,我得到下面给定的终端输出。因为我没有对staff表运行测试,所以我不理解为什么会出现这个错误 有人能告诉我为什么在我没有在那个特定的模型上运行测试的时候,我总是会出现这个错误吗 staff表的模式 create_table "staffs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8

我在staff表上定义了一列“full_name”,该列也设置为不为null。当我运行下面的测试(patient_test.rb)时,我得到下面给定的终端输出。因为我没有对staff表运行测试,所以我不理解为什么会出现这个错误

有人能告诉我为什么在我没有在那个特定的模型上运行测试的时候,我总是会出现这个错误吗

staff表的模式

create_table "staffs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "full_name",   null: false
    t.string   "first_name",  null: false
    t.string   "middle_name"
    t.string   "last_name",   null: false
    t.boolean  "gender",      null: false
    t.string   "telephone",   null: false
    t.string   "email",       null: false
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end
测试文件(patient_Test.rb)

此外,my staff.rb文件中只有关系和作用域(如果有)

Staff.rb文件

class Staff < ApplicationRecord

  has_one :user
  has_many :time_slots

  validates_presence_of :full_name
end

这是因为您对staff表上的大多数列声明了null false条件。Rails在运行每个测试运行之前加载所有设备,因此当设备“staff.yml”被点击时,Rails会立即引发错误,因为设备有两条记录(一条和两条),没有数据库所需的值

您基本上有3种选择:

  • 删除夹具的文件:staff.yml
  • 注释(在该夹具的所有行前面给出“#”符号
  • 为staff one和staff two添加具有值的必需列

  • 测试框架是否正在尝试创建任何装置?如果您使用的是Minitest,则它们位于
    test/fixtures
    @Iceman我为工作人员发布了相应的文件内容。请检查:)谢谢!!你找到了解决办法!对于未来的访问者,这是错误的解决方案。我对这个答案投了赞成票,但遗憾的是,我的赞成票还没有出现。
    rake test
    (in /home/user/Rubymine/project1)
    Run options: --seed 51365
    
    # Running:
    
    E
    
    Error:
    PatientTest#test_should_not_save_user_without_patient:
    ActiveRecord::StatementInvalid: Mysql2::Error: Field 'full_name' doesn't have a default value: INSERT INTO `staffs` (`created_at`, `updated_at`, `id`) VALUES ('2017-03-28 08:45:58', '2017-03-28 08:45:58', 980190962)
    
    
    
    bin/rails test test/models/patient_test.rb:9
    
    
    
    Finished in 0.112169s, 8.9151 runs/s, 0.0000 assertions/s.
    
    1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
    
    class Staff < ApplicationRecord
    
      has_one :user
      has_many :time_slots
    
      validates_presence_of :full_name
    end
    
    # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
    
    # This model initially had no columns defined. If you add columns to the
    # model remove the '{}' from the fixture names and add the columns immediately
    # below each fixture, per the syntax in the comments below
    #
    one: {}
    # column: value
    #
    two: {}
    # column: value