复合键 Laravel 架构构建器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17065112/
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 07:56:48  来源:igfitidea点击:

Composite keys laravel schema builder

phplaravel

提问by Can Geli?

I need two composite primary keys and only one should be AUTO INCREMENTwhat I tried so far:

我需要两个复合主键,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');
            });

None works. Error message:

没有工作。错误信息:

[Exception]
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary ke
y defined (SQL: alter table
kitchenadd primary key kitchen_restaurant_id
_primary(
restaurant_id)) (Bindings: array (
))

[Exception]
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary ke
y defined (SQL: alter table
厨房restaurant_idadd primary key kitchen_restaurant_id
_primary(
)) (Bindings: array (
))

The solution without Schemabuilder: first, I need to add two composite primary keys and then I need to make one of the AUTO INCREMENTbut I think Schemabuilder can't do this.

没有Schemabuilder的解决方案:首先,我需要添加两个复合主键,然后我需要制作一个,AUTO INCREMENT但我认为Schemabuilder不能这样做。

Note:I can do this with SQL, I mean no problem with MySQL

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

Any suggestions?

有什么建议?

Summary:

概括:

What I need is;

我需要的是;

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

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

with Schemabuilder

Schema建设者

回答by jah

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 :)

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

回答by haleksandre

Eloquent's increments() sets the column as an unsigned integer. With that in mind you have multiple way of achieving your desired composite keys.

Eloquent 的 increments() 将列设置为无符号整数。考虑到这一点,您可以通过多种方式实现所需的复合键。

You could use either interger() or unsignedInteger() and setting the autoIncrements to true:

您可以使用 interger() 或 unsignedInteger() 并将 autoIncrements 设置为 true:

 $table->integer($column, $autoIncrement = false, $unsigned = false);

 $table->unsignedInteger($column, $autoIncrement = false);
 //Returns $this->integer($column, $autoIncrement, true);

Then you could tell Eloquent which are your primary keys via primary().

然后你可以通过primary()告诉Eloquent哪些是你的主键。

Your Schema::create() should look like this:

您的 Schema::create() 应如下所示:

 Schema::create('kitchen', function($table) {
     $table->integer('id', true, true);
     $table->integer('restaurant_id')->unsigned(); //or $table->integer('restaurant_id', false, true);
     $table->foreign('restaurant_id')
           ->references('id')->on('restaurants')
           ->onDelete('cascade');
     $table->string('name');
     $table->primary(array('id', 'restaurant_id'));
 });

Assuming your Restaurant table is using $table->increments('id') this should make 'id' and 'restaurant_id' your primary keys while also matching 'restaurant_id' to the Restaurant table 'id'.

假设您的 Restaurant 表正在使用 $table->increments('id') 这应该使 'id' 和 'restaurant_id' 成为您的主键,同时还将 'restaurant_id' 与 Restaurant 表 'id' 匹配。

回答by omarjebari

It makes no sense to have the autoincrement as part of a composite primary key as it will be unique for every record anyway. If you want an autoincrement primary key and a unique composite key on eg 2 other columns such as 'restaurant_id' and 'name':

将自动增量作为复合主键的一部分是没有意义的,因为无论如何它对于每条记录都是唯一的。如果您想要一个自动增量主键和一个唯一的复合键,例如 2 个其他列,例如“restaurant_id”和“name”:

Schema::create("kitchen", function($table) 
{
    $table->increments('id');
    $table->integer('restaurant_id');
    $table->string('name');
    $table->unique(['restaurant_id', 'name']);
});

回答by psmail

Another option from Dayle Rees

Dayle Rees 的另一种选择

Schema::create('example', function($table)
{
    $table->integer('id');
    $table->string('username');
    $table->string('email');
    $keys = array('id', 'username', 'email');
    $table->primary($keys);
});

回答by Alexandre Danault

It seems you've hit something that is not currently possible with Laravel, since ->increments()already sets ->primary(), so when you add it yourself you end up with two PRIMARYclauses in the resulting SQL.

似乎您遇到了 Laravel 当前无法实现的问题,因为->increments()已经设置了->primary(),所以当您自己添加它时,您最终会PRIMARY在生成的 SQL 中得到两个子句。

But you may want to try creating the table with the wrong primary key, dropping it, then recreating it:

但是您可能想尝试使用错误的主键创建表,删除它,然后重新创建它:

Schema::create("kitchen", function($table) 
{
    $table->increments('id');
    $table->integer('restaurant_id');
    $table->string('name');
});

Schema::table('kitchen', function($table)
{
    $table->dropPrimary('kitchen_id_primary');
});

Schema::table('kitchen', function($table)
{
    $table->primary(array('id', 'restaurant_id'));
});