尝试将时间戳添加到现有表中。
根据API文档添加时间戳 https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_timestamps
这是我的迁移代码:
def change
add_timestamps(:products, null: false)
end
出现错误:
*-- add_timestamps(:products, {:null=>false})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: Cannot add a NOT NULL column with default value NULL: ALTER TABLE "products" ADD "created_at" datetime NOT NULL*
我也尝试过这方面的所有解决方案thread https://stackoverflow.com/questions/7542976/add-timestamps-to-an-existing-table
同样的错误...
轨道 5.1.4
红宝石2.4.0
您无法将具有非空约束的列添加到非空表中,因为表中的现有行将立即具有空值,因此条件失败。
相反,分三个步骤引入列:
def change
# add new column but allow null values
add_timestamps :products, null: true
# backfill existing records with created_at and updated_at
# values that make clear that the records are faked
long_ago = DateTime.new(2000, 1, 1)
Product.update_all(created_at: long_ago, updated_at: long_ago)
# change to not null constraints
change_column_null :products, :created_at, false
change_column_null :products, :updated_at, false
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)