如何在迁移 laravel 5.3 中设置不自动递增?
How can I set no auto increment in migration laravel 5.3?
我的代码迁移是这样的:
public function up()
{
Schema::create('satkers', function (Blueprint $table) {
$table->increments('id');
...
});
}
我运行php artisan migrate
。然后,我在数据库管理员中看到。是这样的:
我不希望它自动递增,所以我该如何通过迁移文件来实现。
我该怎么做?
如果你想要 id 而不是自动递增那么你必须使用
$table->integer('id')->unsigned();
如果不需要 id
字段,则从迁移中删除 $table->increments('id');
。
设置为integer()
in migration and set primary key with primary()
:
$table->integer('id')->unsigned();
$table->primary('id');
另一种方式是define primary key in Eloquent model:
Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.
默认情况下,当您创建迁移文件时,它会假设 table 中会有一个自动递增的 ID,但如果您不想要它。
改成这个
$table->integer('id')->unsigned();
$table->primary('id');
保存迁移文件。
通过 php artisan 回滚您的迁移。
然后 运行 php 迁移命令。
我的代码迁移是这样的:
public function up()
{
Schema::create('satkers', function (Blueprint $table) {
$table->increments('id');
...
});
}
我运行php artisan migrate
。然后,我在数据库管理员中看到。是这样的:
我不希望它自动递增,所以我该如何通过迁移文件来实现。
我该怎么做?
如果你想要 id 而不是自动递增那么你必须使用
$table->integer('id')->unsigned();
如果不需要 id
字段,则从迁移中删除 $table->increments('id');
。
设置为integer()
in migration and set primary key with primary()
:
$table->integer('id')->unsigned();
$table->primary('id');
另一种方式是define primary key in Eloquent model:
Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.
默认情况下,当您创建迁移文件时,它会假设 table 中会有一个自动递增的 ID,但如果您不想要它。 改成这个
$table->integer('id')->unsigned();
$table->primary('id');
保存迁移文件。 通过 php artisan 回滚您的迁移。 然后 运行 php 迁移命令。