gpt4 book ai didi

php - 复合键 Laravel 架构生成器

转载 作者:行者123 更新时间:2023-12-02 23:54:03 26 4
gpt4 key购买 nike

我需要两个复合主键,只有一个应该是 AUTO INCREMENT到目前为止我尝试过的:

// 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]<br/>
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary ke<br/>
y defined (SQL: alter table
厨房add primary key kitchen_restaurant_id<br/>
_primary(
Restaurant_id )) (Bindings: array (<br/>
))

没有Schema的解决方案构建器:首先,我需要添加两个复合主键,然后我需要创建 AUTO INCREMENT 之一但我认为Schema构建器无法执行此操作。

注意:我可以使用SQL做到这一点,我的意思是MySQL没有问题

有什么建议吗?

摘要:

我需要的是;

http://oi39.tinypic.com/es91ft.jpg

Schema build 者

最佳答案

您可以通过未准备的方法的一点帮助来做到这一点:

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` )');

这已经过测试,并且有效!

我知道有点晚了,您可能已经继续前进,但其他人可能会遇到这个:)

关于php - 复合键 Laravel 架构生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com