我对数据库结构,laravel orm和约束有一些疑问。
所以首先我有一些表(此代码来自我的数据库转储):
CREATE TABLE `person` (
`person_id` int(10) UNSIGNED NOT NULL,
`first_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`middle_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `worker` (
`worker_id` int(10) UNSIGNED NOT NULL,
`hire_date` date NOT NULL,
`person_id` int(10) UNSIGNED NOT NULL,
`notes` text COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `worker_contract` (
`worker_contract_id` int(10) UNSIGNED NOT NULL,
`notes` text COLLATE utf8mb4_unicode_ci,
`worker_id` int(10) UNSIGNED NOT NULL,
`salary` decimal(10,2) NOT NULL,
`worker_id_boss` int(10) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `worker_specialty` (
`worker_specialty_id` int(10) UNSIGNED NOT NULL,
`worker_id` int(10) UNSIGNED NOT NULL,
`notes` text COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `describe` (
`describe_id` int(10) UNSIGNED NOT NULL,
`person_id` int(10) UNSIGNED DEFAULT NULL,
`describe` text COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `person`
MODIFY `person_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=47;
ALTER TABLE `worker`
MODIFY `worker_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=45;
ALTER TABLE `worker_contact`
MODIFY `worker_contact_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
ALTER TABLE `describe`
MODIFY `describe_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
ALTER TABLE `person`
ADD PRIMARY KEY (`person_id`);
ALTER TABLE `worker`
ADD PRIMARY KEY (`worker_id`),
ADD KEY `person_id_FK` (`person_id`) USING BTREE;
ALTER TABLE `worker_contract`
ADD PRIMARY KEY (`worker_contract_id`),
ADD KEY `worker_id_FK` (`worker_id`) USING BTREE,
ADD KEY `person_id_boss_foreign` (`person_id_boss`);
ALTER TABLE `describe`
ADD PRIMARY KEY (`describe_id`);
ALTER TABLE `worker_contract`
ADD CONSTRAINT `worker_id_boss_foreign` FOREIGN KEY (`worker_id_boss`) REFERENCES `person` (`person_id`) ON DELETE CASCADE;
ALTER TABLE `describe`
ADD CONSTRAINT `person_id_tag_foreign` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`) ON DELETE SET NULL;
这是我的问题,当我从数据库中删除人员表,并且想要在不检查前瞻性键的情况下将其导入时,我有#1005 cannot create 'db'.'person' (Errno: 150 "Foreign key constraint is incorrectly formed")
。
我使用Laravels ORM创建迁移。所有id字段均为相同类型,并且名称字段中没有错误。当我从数据库中删除约束时,它可以工作并且phpMyAdmin没有显示模型之间的关系。当我导入数据库转储时,没有错误。
当我用php代码描述模型时,ORM在哪里使用模型类中的关系?仅在应用程序位置?
当我删除约束时,数据库效率会变小吗?是否需要?
我应该解决的问题或这里出了什么问题?
首先,禁用FOREIGN_KEY_CHECKS
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); // disable
//do your import logic
DB::statement('SET FOREIGN_KEY_CHECKS=1;'); //enable