Php 为什么在我的Laravel迁移中会出现引用错误?
我正在创建一个Laravel电子商务网站,并遵循本教程: 我在第18集,在那里创建了订单表。我正在努力完成它的第一部分,创建迁移。有两个创建迁移文件:Php 为什么在我的Laravel迁移中会出现引用错误?,php,laravel,database-migration,Php,Laravel,Database Migration,我正在创建一个Laravel电子商务网站,并遵循本教程: 我在第18集,在那里创建了订单表。我正在努力完成它的第一部分,创建迁移。有两个创建迁移文件: 2020_07_10_134530_create_orders_table.php(创建订单表) 2020_07_10_135517_create_order_product_table.php(为订单和产品创建透视表) 我的“create_orders_table.php”如下所示: public function up() { Schem
public function up()
{
Schema::create('orders',函数(Blueprint$表){
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users'))
->onUpdate('cascade')->onDelete('set null');
$table->string('billing_email')->nullable();
$table->string('billing_name')->nullable();
$table->string('billing_address')->nullable();
$table->string('billing_city')->nullable();
$table->string('billing_province')->nullable();
$table->string('billing_postalcode')->nullable();
$table->string('billing_phone')->nullable();
$table->string('billing_name_on_card')->nullable();
$table->integer('billing_折扣')->默认值(0);
$table->string('billing_折扣_code')->nullable();
$table->integer('billing_subtotal');
$table->integer('billing_tax');
$table->integer('billing_total');
$table->string('payment_gateway')->默认('stripe');
$table->boolean('shipped')->默认值(false);
$table->string('error')->nullable();
$table->timestamps();
});
}
我的“create_order_product_table.php”如下所示:
public function up()
{
Schema::create('order_product',函数(Blueprint$表){
$table->increments('id');
$table->integer('order_id')->unsigned()->nullable();
$table->foreign('order_id')->references('id'))
->on('orders')->onUpdate('cascade')->onDelete('set null');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id'))
->on('iamlushes')->onUpdate('cascade')->onDelete('set null');
$table->integer('quantity')->unsigned();
$table->timestamps();
});
}
当我执行命令时:
php artisan migrate
我从终端收到以下错误:
rosscurrie@Rosss-Air JanVal % php artisan migrate
Migrating: 2020_07_10_134530_create_orders_table
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table `orders` add constraint `orders_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
+11 vendor frames
12 database/migrations/2020_07_10_134530_create_orders_table.php:38
Illuminate\Support\Facades\Facade::__callStatic("create")
+22 vendor frames
35 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
主要错误似乎是:
SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table `orders` add constraint `orders_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade)
我认为这是指我的“create_orders_table.php”迁移中的这一行代码:
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');
我不确定如何修复此错误,以下是我的“2014_10_12_000000_create_users_table.php”迁移:
public function up()
{
Schema::create('users',函数(Blueprint$表){
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
下面是我的产品表“2020\u 06\u 16\u 124046\u create\u iamlushes\u table.php”的迁移:
public function up()
{
Schema::create('iamlushes',函数(Blueprint$表){
$table->id();
$table->string('name')->unique();
$table->string('fullname')->unique();
$table->string('productLogo');
$table->string('img')->unique();
$table->text('description');
$table->integer('price');
$table->timestamps();
});
}
您正在使用$table->id()代码>在创建用户
表中。它在数据库中创建一个类型为unsinedbiginger的列,但您在orders
表中将此列作为unsignedInteger
引用,这会产生冲突
您应该使用unsignedbiginger
作为orders
表中user\u id
的列类型,如下所示:
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('set null');
....your code
});
您正在使用$table->id()代码>在创建用户
表中。它在数据库中创建一个类型为unsinedbiginger的列,但您在orders
表中将此列作为unsignedInteger
引用,这会产生冲突
您应该使用unsignedbiginger
作为orders
表中user\u id
的列类型,如下所示:
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('set null');
....your code
});
我不是100%,但我猜是->nullable()
导致了以下问题:$table->integer('user_id')->unsigned()->nullable()
,因为引用的列(另一个表的主键)不可为null。对于$table->integer('order_id')->unsigned()->nullable()也是一样的代码>。如果一列的外键指向另一列,则它们必须相同。我不是100%,但我猜是->nullable()
导致了以下问题:$table->integer('user_id')->unsigned()->nullable()
,因为引用的列(另一个表的主键)不可为null。对于$table->integer('order_id')->unsigned()->nullable()也是一样的代码>。如果一列的外键指向另一列,则它们必须相同。是的,这可以解决您的问题。另一种方法是我在答案中发布的更新代码。更改orders
表中user\u id
的列。是的,它可以解决您的问题。另一种方法是我在答案中发布的更新代码。更改orders
表中user\u id
的列。