新引用将不起作用,因为旧记录 Rails Activerecord Postgres

New references won't work because old records Rails Activerecord Postgres

我有 table 辆卡车和 table 清单。 最初我只有清单 table,你用字符串输入卡车的名称。 现在我创建了 table 辆卡车并且我 运行 这个命令

rails g migration AddTruckToChecklists truck:references

稍后添加belongs_to,检查参数、显示、新建等。 In local works perfect, in the checklist you select 来自 table

的卡车

问题是在将更改提交到 Heroku 时出现此错误

PG::NotNullViolation: ERROR: column "truck_id" contains null values

我认为问题是 tablee 核对清单现在有旧值和新列 (truck_id),并且该值为 null 或其他。在本地我没有问题,因为我在数据库中没有旧记录。

我怎样才能给系统赋值,或者让系统在引用中有没有值的旧记录或所有具有相同值或任何其他想法的记录。

只需在迁移中将默认的 null: false 更改为 null:true 即可使其正常工作。 我不知道那是什么后果。

def change
    add_reference :checklists, :truck, null: true, foreign_key: true
end