组合键laravel模式构建器 [英] Composite keys laravel schema builder
问题描述
我需要两个复合主键,并且只有一个应该是到目前为止我尝试过的 AUTO INCREMENT
:
I need two composite primary keys and only one should be AUTO INCREMENT
what I tried so far:
// first try
Schema::create("kitchen", function($table) {
$table->increments('id');
$table->integer('restaurant_id');
$table->primary(array('id', 'restaurant_id'));
$table->string('name');
});
// second try
Schema::create("kitchen", function($table) {
$table->increments('id');
$table->integer('restaurant_id');
$table->primary('restaurant_id');
$table->string('name');
});
均无效.错误消息:
[Exception]
厨房
SQLSTATE [42000]:语法错误或访问冲突:1068多个主ke
y已定义(SQL:更改表添加主键kitchen_restaurant_id
restaurant_id
_primary())(绑定:数组(
))
[Exception]
kitchen
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary ke
y defined (SQL: alter tableadd primary key kitchen_restaurant_id
restaurant_id
_primary()) (Bindings: array (
))
没有 Schema
构建器的解决方案:首先,我需要添加两个复合主键,然后需要制作 AUTO INCREMENT
之一,但我认为架构
构建器无法执行此操作.
The solution without Schema
builder: first, I need to add two composite primary keys and then I need to make one of the AUTO INCREMENT
but I think Schema
builder can't do this.
注意:我可以使用 SQL 来做到这一点,我的意思是MySQL没问题
Note: I can do this with SQL, I mean no problem with MySQL
有什么建议吗?
摘要:
我需要的是
http://oi39.tinypic.com/es91ft.jpg
使用 Schema
构建器
推荐答案
您可以在未准备好的方法的帮助下完成此操作:
You can do this with a little help from the unprepared method:
Schema::create("kitchen", function($table) {
$table->increments('id');
$table->integer('restaurant_id');
$table->string('name');
});
DB::unprepared('ALTER TABLE `kitchen` DROP PRIMARY KEY, ADD PRIMARY KEY ( `id` , `restaurant_id` )');
这已经过测试,并且可以正常工作!
This is tested, and works!
我知道现在有点晚了,您可能已经前进了,但是其他人可能会遇到这个问题:)
I know it's a little late, and you might have moved on, but someone else might come across this :)
这篇关于组合键laravel模式构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!