Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel迁移中创建包含自动增量的多个主键?_Php_Laravel 4 - Fatal编程技术网

Php 如何在laravel迁移中创建包含自动增量的多个主键?

Php 如何在laravel迁移中创建包含自动增量的多个主键?,php,laravel-4,Php,Laravel 4,我刚和拉威尔有了新的关系。我在执行迁移时遇到问题。我的模式就是这样 public function up() { Schema::create('journal', function($table){ $table->increments('id'); $table->timestamp('journal_date'); $table->string('no_ref',25); $table->str

我刚和拉威尔有了新的关系。我在执行迁移时遇到问题。我的模式就是这样

public function up()
{
    Schema::create('journal', function($table){
        $table->increments('id');
        $table->timestamp('journal_date');
        $table->string('no_ref',25);
        $table->string('acc_id', 10);
        $table->string('description', 100);
        $table->integer('debet')->default(0);
        $table->integer('kredit')->default(0);
        $table->primary(array('journal_date', 'no_ref', 'acc_id'));
    });
}
                DB::statement('ALTER TABLE  `journal` DROP PRIMARY KEY , ADD PRIMARY KEY (  `id` ,  `journal_date` ,  `no_ref` ,  `acc_id` ) ;');
然后,当运行PHP artisan migrate时,我得到一个错误

[Illuminate\Database\QueryException]                                                                                                                                                            
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key 
defined (SQL: alter table `journal` add primary key 
journal_journal_date_no_ref_acc_id_primary(`journal_date`,   
`no_ref`, `acc_id`))    

我建议
删除主
,但这也会删除自动增量。我只是不知道怎么弄清楚。最后,我找到了答案。我刚刚使用了这样的DB::语句

public function up()
{
    Schema::create('journal', function($table){
        $table->increments('id');
        $table->timestamp('journal_date');
        $table->string('no_ref',25);
        $table->string('acc_id', 10);
        $table->string('description', 100);
        $table->integer('debet')->default(0);
        $table->integer('kredit')->default(0);
        $table->primary(array('journal_date', 'no_ref', 'acc_id'));
    });
}
                DB::statement('ALTER TABLE  `journal` DROP PRIMARY KEY , ADD PRIMARY KEY (  `id` ,  `journal_date` ,  `no_ref` ,  `acc_id` ) ;');

我的问题解决了。

我找到了这个解决方案,请跟进。中的小更改将创建主键

public function up()
{
//
    Schema::create('journal', function($table){
        $table->unsignedInteger('id');
        $table->timestamp('journal_date');
        $table->string('no_ref',25);
        $table->string('acc_id', 10);
        $table->string('description', 100);
        $table->integer('debet')->default(0);
        $table->integer('kredit')->default(0);
        $table->primary(array('id', 'journal_date', 'no_ref', 'acc_id'));
    });
}

自动增量字段必须是键(例如,如果不是键,MySQL甚至不允许您定义自动增量列)。这可能就是为什么不能简单地删除键的原因。请在删除前为字段定义第二个键。($table->increments('id')->unique();)


相似的帖子:不,不,不同。本例不涉及外键,而是多个主键,包括自动递增欢迎使用StackOverflow。你能描述一下你的代码是如何工作的吗?这样,偶然发现你答案的人就会明白它在做什么,以及它是如何做的?请阅读以更好地理解答案是什么。请同时浏览该网站