gpt4 book ai didi

php - 如何在 laravel 迁移中使用 auto_increment 创建多个主键?

转载 作者:搜寻专家 更新时间:2023-10-31 20:40:13 36 4
gpt4 key购买 nike

我刚刚接触 Laravel。我在进行迁移时遇到问题。我的 Schema 是这样的

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

然后在运行 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`))

我对drop primary 做了一些建议,但是这也会放弃自动递增。我只是不知道怎么弄明白。

最佳答案

我终于找到了答案。我只是像这样使用 DB::statement

                DB::statement('ALTER TABLE  `journal` DROP PRIMARY KEY , ADD PRIMARY KEY (  `id` ,  `journal_date` ,  `no_ref` ,  `acc_id` ) ;');

我的问题解决了。

关于php - 如何在 laravel 迁移中使用 auto_increment 创建多个主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923148/

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