【发布时间】:2018-01-24 03:20:11
【问题描述】:
最近我开始在创建新记录时遇到某些模型的错误:
PG::NotNullViolation:错误:“id”列中的空值违反 非空约束
并非所有型号都会发生这种情况,但只会发生其中一些,而且并不总是立即发生。经过一番检查后,我意识到当 'id: :integer' 出现在表创建行的 schema.rb 文件中时出现错误。
这是一个创建无错误表的迁移示例:
class CreateArticles < ActiveRecord::Migration[5.0]
def change
create_table :articles do |t|
t.string :main_title
t.boolean :published
t.string :short_title
t.text :a_body
t.string :a_image
t.timestamps
end
end
end
这就是 schema.rb 中显示的内容:
create_table "articles", force: :cascade do |t|
t.string "main_title"
t.boolean "published"
t.string "short_title"
t.text "a_body"
t.string "a_image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.string "a_meta_title"
t.string "a_meta_description"
t.string "a_other"
t.string "a_comment"
t.index ["slug"], name: "index_articles_on_slug", using: :btree
end
现在对“代理”做了同样的事情:
class CreateAgents < ActiveRecord::Migration[5.0]
def change
create_table :agents do |t|
t.boolean :status
t.string :stripeId
t.integer :UserID
t.timestamps
end
end
end
但是 schema.rb 文件的结果部分看起来不同:
create_table "agents", id: :integer, force: :cascade do |t|
t.boolean "status"
t.string "stripeId"
t.integer "UserID"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
如您所见,我无法检测到,添加了“id::integer”。我试图将其更改为:serial,转储我的数据库,重新加载架构(在所有类似的 Stack Overflow 问题中都找到了建议),但没有任何帮助。过去,我曾经建立一个新模型,但这不是一个可扩展的解决方案。任何建议将不胜感激。
我还注意到错误只发生在本地,但从不在生产中(我使用 Heroku)。
如果需要,比较模型:
文章型号:
class Article < ApplicationRecord
extend FriendlyId
friendly_id :short_title, use: :slugged
end
代理模型:
class Agent < ApplicationRecord
end
我用:
ruby 2.4.2p198(2017-09-14 修订版 59899)
Rails 5.0.6
psql(9.6.1,服务器 9.6.6)
更新: 为了澄清我在寻找什么 - 我想保留自动生成的 ID,只是防止非空违规错误
【问题讨论】:
-
完全不相关......但这种情况:
t.integer "UserID"可能会在某些时候让出轨(可能还有开发人员)感到困惑。 rails 假定以大写字母开头的方法是类名,而不是方法/列名... rails 标准将调用它:t.integer "user_id"代替:) 甚至可能t.references :user(如果你有User类已经)。 (注意:我当然知道是否有必要出于遗留原因)
标签: ruby-on-rails ruby postgresql ruby-on-rails-5