Laravel 4:如何创建带有迁移的非主要自动递增列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15391850/
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
Laravel 4: How do I create a non-primary auto incrementing column with migrations?
提问by christiaanderidder
I'm currently working on a Laravel 4 project consisting of a master server and many clients. The clients create data and send this to the master server. To avoid conflicts I am using UUID v4 as the primary key.
我目前正在开发一个由主服务器和许多客户端组成的 Laravel 4 项目。客户端创建数据并将其发送到主服务器。为了避免冲突,我使用 UUID v4 作为主键。
However, once the data is created on the server I want to assign a unique auto-incrementing integer so it is easier for users to identify the data. For example: Instead of speaking about item 5a8e896d-3ab4-48d2-9d39-faeb5227f012
a user can speak about item #24567
但是,一旦在服务器上创建了数据,我想分配一个唯一的自动递增整数,以便用户更容易识别数据。例如:item 5a8e896d-3ab4-48d2-9d39-faeb5227f012
用户可以谈论而不是谈论item #24567
To keep my app managable I am using migrations, my current migration for this table looks like this:
为了使我的应用程序可管理,我使用了迁移,我当前对该表的迁移如下所示:
public function up()
{
Schema::table('items', function($table)
{
$table->create();
$table->string('id')->primary(); //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer('number', true); //The human readable item number, the second parameter is true for auto-increment
$table->text('otherdata');
$table->timestamps();
});
}
The problem is that Laravel automagically creates a primary key when defining auto-increment and so the migration ends up failing because there are two primary keys.
问题是 Laravel 在定义自动增量时会自动创建一个主键,因此迁移最终失败,因为有两个主键。
[Exception] SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined
(SQL: alter table `items` add primary key items_id_primary(`id`)) (Bindings: array ())
Is there any way to have a table with a primary key and a seperate auto-incrementing field using Laravel 4 migrations.
有没有办法使用 Laravel 4 迁移来拥有一个带有主键和一个单独的自动递增字段的表。
采纳答案by christiaanderidder
I found the problem, Laravel seems to be creating a primary key for each auto_increment field. WHen i removed the primary key
part it asked me to provide an index so I calling ->unique()
on the migration, but this too did not work. Changing the return ' auto_increment primary key';
to return ' auto_increment unique'
; solved my problem, although it is now hacked in the core, which of course is bad practice.
我发现了问题,Laravel 似乎正在为每个 auto_increment 字段创建一个主键。当我删除primary key
它要求我提供索引的部分时,我呼吁->unique()
进行迁移,但这也不起作用。更改return ' auto_increment primary key';
为return ' auto_increment unique'
; 解决了我的问题,尽管它现在在核心中被黑了,这当然是不好的做法。
/**
* Get the SQL for an auto-increment column modifier.
*
* @param Illuminate\Database\Schema\Blueprint $blueprint
* @param Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
{
if ($column->type == 'integer' and $column->autoIncrement)
{
return ' auto_increment unique'; //return ' auto_increment primary key';
}
}
回答by Camille Guay
trick is to add it outside of the Schema::create like this
技巧是像这样将它添加到 Schema::create 之外
Schema::create('payments', function(Blueprint $table)
{
$table->string('primaryKey', 30);
$table->primary('primaryKey');
//...
});
DB::statement('ALTER Table tableName add id INTEGER NOT NULL UNIQUE AUTO_INCREMENT;');
Then redo migrations, the key will create with name id in the table tableName then you can access it like any other key.
然后重做迁移,键将在表 tableName 中创建名称为 id 的键,然后您可以像访问任何其他键一样访问它。
回答by Nikhil Agrawal
I think it cannot be done without modifying the core files, because creating a auto_ increment automatically makes it as a primary key.
我认为不修改核心文件就无法完成,因为创建 auto_ increment 会自动使其成为主键。
Better if u can report it as bug in Larvel framework dev. team.
如果您可以将其报告为 Larvel 框架开发中的错误,那就更好了。团队。
hereThanks :)
在这里谢谢:)
回答by Ajay Patel
yes you can do it in your Model declaration of your Items class
是的,您可以在 Items 类的模型声明中执行此操作
class Items extends Eloquent {
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
}
now your primary key is no more auto incremented field.
现在您的主键不再是自动递增的字段。