我有一个带有locations表的数据库,其中包含表示给定位置的地址的字符串字段-第一行,邮政编码,城市(该表使用timestamps
和softDeletes
)。我现在正在创建一个city表,其中给定位置将通过外键连接到city。
此迁移的模式表示为:
Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
$table->softDeletes();
});
Schema::table('locations', function ($table) {
$table->integer('city_id')->unsigned()->nullable();
$table->foreign('city_id')->references('id')->on('cities');
});
不幸的是,这给了我以下错误:
Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'deleted_
at' at row 154 (SQL: alter table `locations` add constraint `locations_city_id_foreign` foreign key (`city_id`) ref
erences `cities` (`id`))
现在,有一种简单的方法可以解决此问题,方法是在创建城市表和创建索引之间运行以下操作:
DB::table('locations')->where('deleted_at', "0000-00-00 00:00:00")->update([
'deleted_at' => Carbon::now()
]);
问题是-为什么在不相关列上创建索引会检查Deleted_at上的值?
每个评论,这里是SHOW CREATE TABLE locations
的当前输出:
CREATE TABLE `locations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`token` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`external_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_pickup_choice` tinyint(1) DEFAULT '0',
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`address_line_1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`address_line_2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`town` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`county` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`postcode` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`longitude` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`latitude` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`type_of_address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_pickup` tinyint(1) DEFAULT '0',
`is_dropoff` tinyint(1) DEFAULT '0',
`is_live` tinyint(1) DEFAULT '0',
`is_actual_point` tinyint(1) DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`is_near_public_transport` tinyint(1) DEFAULT '0',
`public_transport_notes` longtext COLLATE utf8_unicode_ci,
`notes` longtext COLLATE utf8_unicode_ci,
`url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_fictional` tinyint(1) DEFAULT '0',
`city_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `locations_latitude_index` (`latitude`),
KEY `locations_longitude_index` (`longitude`),
KEY `locations_deleted_at_index` (`deleted_at`),
KEY `locations_city_id_foreign` (`city_id`),
CONSTRAINT `locations_city_id_foreign` FOREIGN KEY (`city_id`) REFERENCES `cities` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8440 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci