Lor*_*aef 0 php postgresql laravel eloquent
我有一个带有复合主键的表.我想创建一个迁移,删除该主键,并引入一个新的列'id',它是顺序的,将成为新的主键.
Schema::table('cart_line', function (Blueprint $table) {
$table->dropPrimary('cart_line_pkey');
$table->increments('id');
$table->primary('id');
});
Run Code Online (Sandbox Code Playgroud)
此代码抛出以下错误:
SQLSTATE[42P16]: Invalid table definition: 7 ERROR: multiple primary keys for table "cart_line" are not allowed (SQL: alter table "cart_line" add primary key ("id"))
Run Code Online (Sandbox Code Playgroud)
如果我直接执行以下SQL,则删除主键并执行上面的代码而不会出现错误:
ALTER TABLE cart_line DROP CONSTRAINT cart_line_pkey;
Run Code Online (Sandbox Code Playgroud)
这就是我尝试的原因
DB::statement('ALTER TABLE cart_line DROP CONSTRAINT cart_line_pkey;');
Schema::table('cart_line', function (Blueprint $table) {
$table->increments('id');
$table->primary('id');
});
Run Code Online (Sandbox Code Playgroud)
但是抛出了同样的错误.旧主键不会被删除,但在尝试创建新主键之前没有错误.
我正在使用laravel 5.5和postgres 9.6
删除$table->primary('id') 因为 $table->increments('id')使id列成为主键
Schema::table('cart_line', function (Blueprint $table) {
$table->dropPrimary('cart_line_pkey');
$table->increments('id');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
492 次 |
| 最近记录: |