gpt4 book ai didi

mysql - Rails - MySQL 自动递增 id 列类型更改

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:05 24 4
gpt4 key购买 nike

我有一个包含大量模型和数据库表的 Rails 网站。其中,我想将 :id 主键列更改为 bigint 而不是 int,因为它即将超过 MySQL 的整数 int(11) 限制。

我不想使用其他帖子中建议的方法,他们建议我们在创建表和添加自定义列时执行 :id => false :id 指定大小。

我已经有很多数据,我不希望 id 列 id 也发生变化,因为我将它用于一些 has_many belongs_to 关系并且可能会破坏所有链接。

请建议我如何在不重新创建表和/或丢失数据和 ID 值的情况下将_column id 从 int(11) 更改为 bigint

最佳答案

不要忘记 auto_increment: true

class NameOfYourMigration < ActiveRecord::Migration
def up
change_column :table_name, :id, :integer, limit: 8, auto_increment: true
end

def down
change_column :table_name, :id, :integer, auto_increment: true
end
end

关于mysql - Rails - MySQL 自动递增 id 列类型更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com