15

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

没有Schema构建器的解决方案:首先,我需要添加两个复合主键,然后我需要制作一个,AUTO INCREMENT但我认为Schema构建器无法做到这一点。

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

有什么建议么?

概括:

我需要的是;

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

Schema建设者

4

6 回答 6

15

您可以在未准备好的方法的帮助下做到这一点:

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

这是经过测试的,并且有效!

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

于 2013-11-17T16:51:38.760 回答
7

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

Schema::create("kitchen", function($table) 
{
    $table->increments('id');
    $table->integer('restaurant_id');
    $table->string('name');
    $table->unique(['restaurant_id', 'name']);
});
于 2016-03-31T16:00:56.060 回答
6

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

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

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

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

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

您的 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'));
 });

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

于 2014-11-17T20:16:52.983 回答
2

Dayle Rees的另一个选择

Schema::create('example', function($table)
{
    $table->integer('id');
    $table->string('username');
    $table->string('email');
    $keys = array('id', 'username', 'email');
    $table->primary($keys);
});
于 2014-05-11T06:56:28.430 回答
1

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

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

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'));
});
于 2013-06-12T18:41:00.573 回答
0

你可以这样尝试

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

如果你还没有安装学说/dbal 包。请先安装学说/dbal 包。更改表的列需要学说/dbal 包。在框架的根目录上运行此命令:

composer require doctrine/dbal
于 2021-06-27T15:49:06.933 回答