我正在使用mysql数据库。我有两个具有一对多关系的表模式(客户机和产品模型)。
Schema::create('clients', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('job');
$table->timestamps();
});
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->foreignId('client_id')->constrained();
$table->string('productName')->unique();
$table->integer('quantity');
$table->float('unitPrice');
$table->timestamps();
});
我还尝试使用下面的语法,而不是
` $table->foreignId('client_id')->constrained();`
但我也犯了同样的错误。
`$table->unsignedBigInteger('client_id');
$table->foreign('client_id')->references('id')->on('clients');`
错误如下:
SQLSTATE[HY000]: General error: 1005 Can't create table `product`.`products` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL:
alter table `products` add constraint `products_client_id_foreign` foreign key (`client_id`) references `clients` (`id`))
暂无答案!
目前还没有任何答案,快来回答吧!