我知道这个问题已经问了很多,我已经尝试了所有答案,但似乎无法调试遇到的这个问题。
我有一个名为create_product_image_table的迁移
class CreateProductImageTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_image', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
$table->string('image_url');
$table->timestamps();
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_image');
}
}
另一个迁移称为create_products_table
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('product_name');
$table->text('product_description');
$table->decimal('product_cost', 8, 2);
$table->decimal('product_price', 8, 2);
$table->bigInteger('unit_sold')->nullable();
$table->bigInteger('UPC')->nullable();
$table->unsignedBigInteger('product_image_id')->nullable();
$table->timestamps();
$table->foreign('product_image_id')
->references('id')
->on('product_image')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
它们都具有相同的无符号大整数类型。更改了创建迁移的日期并删除了表以及后来的数据库本身的日期之后,我似乎无法传递Errno 150错误。
感谢您的帮助。
托马斯
答案 0 :(得分:1)
在AppServiceProvider中添加默认字符串长度
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}
答案 1 :(得分:1)
您的products_table
与product_image_table
有关系,
product_image_table
属于products_table
。
因此,您只需要在product_image_table
上定义关系。
您需要首先创建products_table
,它看起来像:
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('product_name');
$table->text('product_description');
$table->decimal('product_cost', 8, 2);
$table->decimal('product_price', 8, 2);
$table->bigInteger('unit_sold')->nullable();
$table->bigInteger('UPC')->nullable();
$table->unsignedBigInteger('product_image_id')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
然后您需要创建product_image_table
,它看起来像:
class CreateProductImageTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_image', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
$table->string('image_url');
$table->timestamps();
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_image');
}
}