Doctorine/Dbal - 更新表迁移在整数上添加不需要的“自动增量”(不是增量)

Posted

技术标签:

【中文标题】Doctorine/Dbal - 更新表迁移在整数上添加不需要的“自动增量”(不是增量)【英文标题】:Doctorine/Dbal - Update table migration adding unwanted "Auto Increment" on integer (not increments) 【发布时间】:2020-11-29 22:31:10 【问题描述】:

我正在使用 Laravel 6.6.0 和 Doctrine/Dbal 2.10。

我需要更新迁移,并已按照docs 中的信息进行操作。

我有一个小的无符号非自动递增整数,我需要将其更改为整数。

我实际上希望它是 mediumint,但我从 Laravel 文档中了解到这是不支持的。

只有以下列类型可以“更改”:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger 和 unsignedSmallInteger。 我的初始迁移如下:

...
$table->bigIncrements('id');
$table->smallInteger('membership_code')->unsigned();
$table->char('name')->nullable();
...

在安装了 dbal 包后,我正在尝试以下迁移来更新 membership_code 列:

$table->integer('membership_code', 5)->unsigned()->change();

但是当我运行 migrate 命令时,我得到以下信息:

Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: ALTER TABLE member_centres CHANGE membership_code membership_code INT UNSIGNED AUTO_INCREMENT NOT NULL

我不明白为什么要在迁移中添加AUTO_INCREMENT

我没有 increments 类型,为什么要添加它?

【问题讨论】:

【参考方案1】:

integer 的第二个参数不是大小,它是一个布尔值,表示它是否应该是一个自动增量字段。

public function integer($column, $autoIncrement = false, $unsigned = false)

【讨论】:

谢谢!你刚刚帮我省了几个小时把头发扯下来的时间!只见树木不见森林!

以上是关于Doctorine/Dbal - 更新表迁移在整数上添加不需要的“自动增量”(不是增量)的主要内容,如果未能解决你的问题,请参考以下文章

迁移到新的 SQL Server 后,在 MS Access 中更新链接表的最佳方法是啥?

从版本 3 更新到 4 时,Flyway 更新不会迁移 schema_version 表

WAMP 更新和迁移后,mysql 表没有结构/主键

如何使用 .net 核心实体框架迁移更新表(而不是重新创建它们)

Rails迁移以将主键添加到现有表

EF-使用迁移技术让程序自动更新数据库表结构