【发布时间】:2018-03-19 20:12:32
【问题描述】:
我正在尝试运行 php artisan migrate 以使用 laravel 创建我的 mysql 表。
我收到了这个错误: 外键约束格式不正确
用户表:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('street');
$table->string('city');
$table->string('phone');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
密码重置表:
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
产品表:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_type');
$table->integer('quantity');
$table->timestamps();
});
出货表:
Schema::create('shipments', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_number')->unsigned();
$table->integer('product_id')->unsigned();
$table->foreign('order_number')->references('id')->on('orders');
$table->foreign('product_id')->references('id')->on('products');
$table->dateTime('chargecardtime');
$table->dateTime('packingtime');
$table->date('shiporderdate');
$table->timestamps();
});
订单表:
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('customer_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->foreign('customer_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
$table->string('name');
$table->string('to_street');
$table->string('to_city');
$table->date('ship_date');
$table->string('phone');
$table->timestamps();
});
异常跟踪:
1 PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表ec.#sql-3664_86 (errno: 150 "外键约束格式不正确")")
我猜orders表有问题,因为出错后我在database.others中看不到该表。
【问题讨论】:
-
哪些语句会失败?
-
我看不到
-
我猜orders表有问题,因为出错后我在database.others中看不到该表。