ruby 模型上的时间戳问题-期望值因环境而异

zvms9eto  于 2023-05-17  发布在  Ruby
关注(0)|答案(1)|浏览(176)

我有一张table:

create_table "gocardless_runner_infos", id: :serial, force: :cascade do |t|
    t.integer "runner_id", null: false
    t.string "redirect_flow_id"
    t.string "redirect_flow_url"
    t.string "mandate_id"
    t.string "customer_id"
    t.datetime "timestamp", precision: nil, null: false
  end

如果我尝试用GocardlessRunnerInfo.create(runner_id: id, timestamp: DateTime.now)创建一个示例,我会得到一个错误unknown attribute 'timestamp' for GocardlessRunnerInfo
如果我尝试在不设置时间戳的情况下创建它,那么GocardlessRunnerInfo.create(runner_id: id),那么它将在我的开发和生产环境中工作,但我的测试将出错:

PG::NotNullViolation: ERROR:  null value in column "timestamp" of relation "gocardless_runner_infos" violates not-null constraint
      DETAIL:  Failing row contains (21, 1044890529, null, null, null, null, null).
       (ActiveRecord::NotNullViolation)

这就像我的测试模式需要时间戳,但开发和生产不需要。知道发生了什么吗
数据库为postgres

f0ofjuux

f0ofjuux1#

您的测试数据库是按预期运行的数据库:

PG::NotNullViolation: ERROR:  null value in column "timestamp" of relation "gocardless_runner_infos"

因为你已经设置了null: false

t.datetime "timestamp", precision: nil, null: false

有几个潜在的原因:
1.您的developmentproduction数据库没有此not null约束。
1.您的developmentproduction数据库不是Postgres,因此约束的应用方式不同
1.您的developmentproduction数据库有一个默认值,如"timestamp" TIMESTAMP(6) DEFAULT NOW() NOT NULL,它在创建时设置该值,以便满足not null约束。

相关问题