我目前正在致力于一个由主服务器和许多客户端组成的Laravel 4项目。客户端创建数据并将其发送到主服务器。为了避免冲突,我使用UUID v4作为主键。Laravel 4:如何使用迁移创建非主自动递增列?
但是,一旦在服务器上创建了数据,我想分配一个唯一的自动递增整数,以便用户更容易识别数据。例如:不要谈论item 5a8e896d-3ab4-48d2-9d39-faeb5227f012
的用户可以谈论item #24567
为了让我的应用程序可管理我使用的迁移,我对这个表当前迁移看起来是这样的:
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();
});
}
的问题是,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())
是否有任何方式使用Laravel 4迁移有一个主键和一个单独的自动递增字段的表。
我不这么认为,这是更好的解决方案(改变laravel Db类的核心功能)。 – 2013-03-25 09:15:49
如果您已经阅读我的评论,您会理解您的解决方案并未回答我的问题。这就是为什么我低估了它。我知道我的解决方案不是最佳实践,但是你的解决方案并不能解决我的问题。 – christiaanderidder 2013-03-25 13:52:15
所以你需要两个单独的列,一个是自动递增的主键和另一个uuid键。我对吗? – 2013-03-25 13:58:55