【发布时间】:2019-08-23 12:00:07
【问题描述】:
迁移名为“存档”的模型后,所有测试都失败并显示 ActiveRecord / 错误消息,如下所示:
ActiveRecord::NotNullViolation: SQLite3::ConstraintException: NOT NULL constraint failed: archives.created_at: INSERT INTO "archives" ("some_thing") VALUES ('MyString')
这是在运行全新安装的 Ubuntu 18.04.2 和 Ruby 2.6.1 和 Rails 5.2.3 的新 VM 上的新 Rails 应用程序中隔离的,遵循 Odin 项目的安装说明。该问题仅在名为“存档”的模型的测试期间出现,并且删除模型的属性只会将错误更改为:
ActiveRecord::StatementInvalid: SQLite3::SQLException: incomplete input: INSERT INTO "archives"
我的工作流程如下:
rails new sample_app
cd sample_app
(在撰写本文时将 Gemfile 中的 sqlite3 更改为使用版本 '~> 1.3.6' 来修复错误)
bundle install
rails g model Archive
生成此迁移:
class CreateArchives < ActiveRecord::Migration[5.2]
def change
create_table :archives do |t|
t.timestamps
end
end
end
rails db:migrate
创建此架构:
ActiveRecord::Schema.define(version: 2019_04_03_003144) do
create_table "archives", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
rails db:test
运行此测试:(未注释)
require 'test_helper'
class ArchiveTest < ActiveSupport::TestCase
test "the truth" do
assert true
end
end
运行测试后,通常是给定的通过,Rails 返回上面列出的第一条 ActiveRecord 错误消息。 我觉得我在这里遗漏了一些东西,使我无法对模型使用“存档”这个词。
【问题讨论】:
-
也分享给你 schema.rb,专门用于“存档”和你的测试文件
-
刚刚在问题中添加了架构、迁移和模型测试。
-
根据this list,看起来'archive'可能是Rails中的保留字。
标签: ruby-on-rails activerecord sqlite