从现有表的增量迁移到bigIncrements

时间:2019-11-03 15:31:31

标签: laravel laravel-5

我一直在laravel问题/论坛中搜索如何使用现有表将更改从increments()迁移到bigIncrements()

错误:SQLSTATE[HY000]: General error: 1833 Cannot change column 'id': used in a foreign key constraint 'account_users_acc_id_foreign' of table 'mydatabasename.account_users' (SQL: ALTER TABLE accounts CHANGE id id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL)

由于外部表,我知道它的存在,然后我尝试使用Schema::disableForeignKeyConstraints()

禁用约束

错误:SQLSTATE[HY000]: General error: 1025 Error on rename of './mydatabasename/#sql-ea_201' to './mydatabasename/accounts' (errno: 150 - Foreign key constraint is incorrectly formed) (SQL: ALTER TABLE accounts CHANGE id id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL)

在我的up()函数中:

enter image description here

我还试图切换循环的顺序

错误:SQLSTATE[HY000]: General error: 1832 Cannot change column 'acc_id': used in a foreign key constraint 'account_users_acc_id_foreign' (SQL: ALTER TABLE account_users CHANGE acc_id acc_id BIGINT UNSIGNED DEFAULT NULL, CHANGE app_user_id app_user_id BIGINT UNSIGNED NOT NULL, CHANGE approved_by approved_by BIGINT UNSIGNED DEFAULT NULL, CHANGE rejected_by rejected_by BIGINT UNSIGNED DEFAULT NULL)

有什么办法解决这个问题?



参考文献:

DBAL学说:https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/schema-manager.html

2 个答案:

答案 0 :(得分:1)

可以吗?

  1. 将每个表的外键存储在[$table_name => $array_of_foreign_keys]的数组中
  2. 删除所有外键
  3. 将每个表的id列更改为使用bigIncrements
  4. 将每个外键列更改为类型unsigned big integer
  5. 从步骤1开始遍历数组,重新创建所有外键(通过按表名遍历每个外键)

答案 1 :(得分:0)

我尝试实现建议的@brice。现在就可以了。

下面是迁移代码。我不知道这是否是最佳做法

这是我的要旨:https://gist.github.com/afiqiqmal/6518a2048246cd76c03bdee04ff87a82

enter image description here