如何在迁移 laravel 5.3 中不设置自动增量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41375749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:01:36  来源:igfitidea点击:

How can I set no auto increment in migration laravel 5.3?

phpmysqllaravelmigrationlaravel-5.3

提问by samuel toh

My code migration is like this :

我的代码迁移是这样的:

public function up()
{
    Schema::create('satkers', function (Blueprint $table) {
        $table->increments('id');
        ...
    });
}

I run php artisan migrate. Then, I see in database administrator. It's like this : enter image description here

我跑php artisan migrate。然后,我在数据库管理员中看到。就像这样 :在此处输入图片说明

I do not want it to be auto incremented, so how can i do it through migration file.

我不希望它自动递增,所以我如何通过迁移文件来做到这一点。

How can I do it?

我该怎么做?

采纳答案by Alexey Mezenin

Set it as integer()in migration and set primary key with primary():

将其设置为integer()迁移并设置主键primary()

$table->integer('id')->unsigned();
$table->primary('id');

Another way is to define primary key in Eloquent model:

另一种方法是在 Eloquent 模型中定义主键

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.

Eloquent 还将假设每个表都有一个名为 id 的主键列。你可以定义一个 $primaryKey 属性来覆盖这个约定。

此外,Eloquent 假设主键是一个递增的整数值,这意味着默认情况下主键会自动转换为 int。如果您希望使用非递增或非数字主键,则必须将模型上的公共 $incrementing 属性设置为 false。

回答by Md. Abu Taleb

if you want id but not auto increment then you have to use

如果你想要 id 但不是自动递增,那么你必须使用

$table->integer('id')->unsigned();

If idfield is not required then remove $table->increments('id');from the migration.

如果id不需要字段,$table->increments('id');则从迁移中删除 。

回答by rahulsnegi

By default when you create a migration file it will assume that there will be a auto incremented id in the table, but in case you don't want it . change it to this

默认情况下,当您创建迁移文件时,它会假定表中会有一个自动递增的 id,但以防万一您不想要它。改成这个

$table->integer('id')->unsigned();
$table->primary('id');

save your migation file. rollback your migration through php artisan. and then run php migrate command.

保存您的迁移文件。通过 php artisan 回滚您的迁移。然后运行 ​​php migrate 命令。