【发布时间】:2021-02-08 04:21:10
【问题描述】:
我正在 laravel 上创建一个新的应用程序,我正在编写迁移,我想为我的列设置外键,所以我这样做如下:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->integer('type_id');
$table->string('name');
$table->integer('status_id')->default(0);
$table->integer('category_id')->default(0);
$table->integer('store_id');
$table->timestamps();
$table->foreign('status_id')->references('id')->on('product_statuses');
$table->index('status_id');
$table->foreign('type_id')->references('id')->on('product_types');
$table->index('type_id');
$table->foreign('category_id')->references('id')->on('product_categories');
$table->index('category_id');
$table->foreign('store_id')->references('id')->on('stores');
$table->index('store_id');
但这些不起作用,因为我在phpmyadmin 中检查它,它让我插入任何数字,而不是来自status_id 的项目,例如,当我在design 选项卡中检查它时,我看不到表之间的关系。
#编辑
添加product_types 迁移:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
关于我使用 wamp 和 mysql v8 的引擎,我认为它支持 fk 功能
【问题讨论】:
-
我认为 fk 列应该是
unsigned。即$table->integer('status_id')->unsigned()或$table->bigInteger('status_id')->unsigned() -
@porloscerrosΨ 我不走运,但在 phpmyadmin 上它们之间没有关系
-
请向我们展示
product_statuses迁移。并确认您的数据库引擎支持外键。即 mysql 的 Innodb(而不是 myisam) -
请检查我已将迁移添加到问题中
-
对不起,我看到的是在表格的 phpmyadmin 中有一列正在写 :Type :MyISAM 。引擎的意思是一样的吗?