mysql - 从 MySQL 迁移到 MariaDB - 外键约束形成不正确

标签 mysql foreign-keys mariadb mysql-error-150

我刚刚更新了使用 XAMPP 的本地开发环境,新版本的 XAMPP 使用 MariaDB,而我使用的旧版本使用的是 MySQL,我我很好。

现在,我认为 MariaDB 应该与 MySQL 完全兼容,因为它本质上只是一个“直接”替代品,但是我在导入升级前直接从 MySQL 导出的数据库时遇到了问题。

我收到以下错误:

Query:
/*Table structure for table `blm_wc_download_log` */
CREATE TABLE `blm_wc_download_log` (
  `download_log_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `timestamp` datetime NOT NULL,
  `permission_id` bigint(20) unsigned NOT NULL,
  `user_id` bigint(20) unsigned DEFAULT NULL,
  `user_ip_address` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
  PRIMARY KEY (`download_log_id`),
  KEY `permission_id` (`permission_id`),
  KEY `timestamp` (`timestamp`),
  CONSTRAINT `fk_blm_wc_download_log_permission_id` FOREIGN KEY (`permission_id`) REFERENCES `blm_woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci

Error occured at:2019-02-26 05:30:20
Line no.:9919
Error Code: 1005 - Can't create table `my-db`.`blm_wc_download_log` (errno: 150 "Foreign key constraint is incorrectly formed")

这是blm_woocommerce_downloadable_product_permissions:

CREATE TABLE `blm_woocommerce_downloadable_product_permissions` (
  `permission_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `download_id` varchar(36) COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `product_id` bigint(20) unsigned NOT NULL,
  `order_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `order_key` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `user_email` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `user_id` bigint(20) unsigned DEFAULT NULL,
  `downloads_remaining` varchar(9) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `access_granted` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `access_expires` datetime DEFAULT NULL,
  `download_count` bigint(20) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`permission_id`),
  KEY `download_order_key_product` (`product_id`,`order_id`,`order_key`(16),`download_id`),
  KEY `download_order_product` (`download_id`,`order_id`,`product_id`),
  KEY `order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

可能是因为 blm_woocommerce_downloadable_product_permissions 是在文件中更下方创建的,因为它认为该表不存在而出错?但话说回来,我以前从未遇到过将此数据库的 SQL 转储导入 MySQL 的任何问题。

这里有什么问题?如果存在兼容性问题,不妨坚持使用 MySQL...

最佳答案

是的,很可能您在应用 FK 之前没有创建表,您可以注释掉创建表的约束行,然后在其完全创建后应用约束。

关于mysql - 从 MySQL 迁移到 MariaDB - 外键约束形成不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54875134/

相关文章:

python 到 mysql (MariaDB) 插入带有变量错误的查询

mysql - 在 mysql 中连接到 Master 的客户端机器的复制问题

javascript - Node.js 应用程序中的 SequelizeConnectionError

c++ - MySQL C++ 连接器始终输出相同的字符串

Django 将外键限制为满足特定条件的记录

mysql - 当链接为外键时,将主键从 BigInt 更改为 Unsigned BigInt

MYSQL - 查找列是否具有不同的值..?

php - 如何使用外键将值保存到数据库

mysql - 玛丽亚数据库 : Relay log write failure

python - 从数据库列创建行