ActiveRecord :: NotNullViolation-Mysql2列不能为空

问题描述 投票:1回答:1

通过FactoryBot创建记录时遇到问题,该问题仅在test环境下发生。

当我在development控制台上运行命令时:

FactoryBot.create(:accounting_pbs_reservation, factor: 3.0)(列factor也在工厂定义,但我想显式传递它)

然后正确执行并创建了它,但是当我在test环境下运行命令时,它会显示错误:

ActiveRecord::NotNullViolation:
  Mysql2::Error: Column 'factor' cannot be null: 
INSERT INTO `accounting_pbs_reservations` (`start`, `rl_walltime`, `factor`) 
VALUES (1503468000, 1430000, 3.0)

即使显示factor列也是如此。

当我想在RSpec控制器中创建模型的实例时,也会发生此问题。

有人遇到同样的问题吗? testdevelopment]的数据库模式

factor的数据库模式设置为

| Field  | Type   | Null | Key | Default | Extra          
| factor | double | NO   |     | 1       |                

FactoryBot定义很漂亮:

FactoryBot.define do
  factory :accounting_pbs_reservation do
    factor { 1.0 }
    start { 1_503_468_000 }
    rl_walltime { 1_430_000 }
  end
end

发生错误的RSpec定义:

require 'rails_helper'

RSpec.describe AccountingPbsReservationsController, type: :controller do
  let(:valid_accounting_pbs_reservation) { FactoryBot.create(:accounting_pbs_reservation) }
  let(:valid_attributes) { FactoryBot.attributes_for(:accounting_pbs_reservation) }

...
    context 'when logged as admin' do
      login_admin
      it "returns a success response" do
        get :show, params: { id: valid_accounting_pbs_reservation.to_param         expect(response).to be_successful
      end
...

我通过FactoryBot创建记录时遇到问题,该问题仅在测试环境中发生。当我在开发控制台上运行命令时:FactoryBot.create(:...

ruby-on-rails testing rspec factory-bot
1个回答
0
投票

所以问题出在数据库触发器上。

© www.soinside.com 2019 - 2024. All rights reserved.