【发布时间】:2021-04-05 13:59:46
【问题描述】:
我坚持这一点:
我得到了一些这样的迁移:
<?php
Schema::create('product_categories', function ( $table) {
$table->bigIncrements('id');
$table->timestamps();
});
Schema::create('products', function ( $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id')->nullable();
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
});
这会导致:
CREATE TABLE `product` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`category_id` bigint(20) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `product_category_id_foreign` (`category_id`),
CONSTRAINT `product_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `product_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
但如果我删除 product 行,category-row 将保持不变。
知道我犯了什么错误吗?
【问题讨论】:
-
你到底想达到什么目的?您有 3 个表产品、类别和数据透视表产品类别?
-
反之亦然。如果您删除一个类别,该类别的所有产品也将被删除。
标签: sql laravel eloquent mariadb