Laravel数据库迁移中integer类型能否自定义长度?

2026-04-02 04:061阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计247个文字,预计阅读时间需要1分钟。

Laravel数据库迁移中integer类型能否自定义长度?

在Laravel数据库迁移中,若integer类型无法指定长度,许多小伙伴在传递第二个参数后会发现迁移报错,错误信息如下:Syntax error or access violation: 1075 Incorrect table definition; there can be only one。这是因为MySQL不支持为integer类型指定长度。解决方法是在迁移文件中直接使用`integer()`函数,无需指定长度。例如:

phpSchema::create('users', function (Blueprint $table) { $table->integer(); $table->timestamps();});

laravel数据库迁移中integer类型是无法指定长度的,很多小伙伴对integer类型传递第二个参数后会发现迁移报以下错误

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代码后发现通过integer指定长度创建的子段自动添加了auto increament 以及 primary key 属性

Laravel数据库迁移中integer类型能否自定义长度?

int not null auto_increment primary key

查看源代码后发现integer方法的第二个参数并不是指定长度,而是是否设置auto increment,所以integer方法无法指定子段长度,默认为11。

public function integer($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned')); }

以上这篇关于laravel 数据库迁移中integer类型是无法指定长度的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计247个文字,预计阅读时间需要1分钟。

Laravel数据库迁移中integer类型能否自定义长度?

在Laravel数据库迁移中,若integer类型无法指定长度,许多小伙伴在传递第二个参数后会发现迁移报错,错误信息如下:Syntax error or access violation: 1075 Incorrect table definition; there can be only one。这是因为MySQL不支持为integer类型指定长度。解决方法是在迁移文件中直接使用`integer()`函数,无需指定长度。例如:

phpSchema::create('users', function (Blueprint $table) { $table->integer(); $table->timestamps();});

laravel数据库迁移中integer类型是无法指定长度的,很多小伙伴对integer类型传递第二个参数后会发现迁移报以下错误

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代码后发现通过integer指定长度创建的子段自动添加了auto increament 以及 primary key 属性

Laravel数据库迁移中integer类型能否自定义长度?

int not null auto_increment primary key

查看源代码后发现integer方法的第二个参数并不是指定长度,而是是否设置auto increment,所以integer方法无法指定子段长度,默认为11。

public function integer($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned')); }

以上这篇关于laravel 数据库迁移中integer类型是无法指定长度的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。