mysql - 序列化 uuid 的外键给出错误 1215

标签 mysql node.js sequelize.js

给出以下迁移片段:

const uuid = require('uuid');
queryInterface.createTable('listings', {
        id: {
          type: Sequelize.UUID,
          defaultValue: () => uuid.v4(),
          primaryKey: true,
          allowNull: false,
          isUnique: true,
        },
});

还有另一个:

queryInterface.createTable('listing_detail_info', {
        id: {
          type: Sequelize.INTEGER(11),
          primaryKey: true,
          autoIncrement: true,
          allowNull: false,
          isUnique: true,
        },
        listingId: {
          type: Sequelize.UUID,
          isUnique: true,
          allowNull: false,
          field: 'listing_id',
          references: { model: 'listings', key: 'id' },
        },
});

发生以下错误:

 code: 'ER_CANNOT_ADD_FOREIGN',
     errno: 1215,
     sqlState: 'HY000',
     sqlMessage: 'Cannot add foreign key constraint',
     sql:
      'CREATE TABLE IF NOT EXISTS `listing_detail_info` (`id` INTEGER(11) NOT NULL auto_increment , `listing_id` CHAR(36) BINARY NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`listing_id`) REFERENCES `listings` (`id`)) ENGINE=InnoDB;' },

我很确定它们是相同的数据类型(都是 Sequleize.UUID),并且上一个表已成功创建,但不知道为什么仍然会发生这种情况...

最佳答案

试试这个

queryInterface.createTable('listing_detail_info', {
        id: {
          type: Sequelize.INTEGER(11),
          primaryKey: true,
          autoIncrement: true,
          allowNull: false,
          isUnique: true,
        },
        listingId: {
          type: Sequelize.UUID,
          isUnique: true,
          allowNull: false,
        },
});

如果给定的解决方案有效,那么添加外键约束就会出现问题。

关于mysql - 序列化 uuid 的外键给出错误 1215,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55389826/

相关文章:

postgresql - Postgres 和 Sequelize 的 Heroku 部署问题

mysql - 如何从该特定数据库布局中的歌曲表中选择 Songnames.id?

node.js - 如何使用 Seneca 和 Express 发送响应

node.js - 如何通过 NodeJS 在文件中一位一位地写入和读取?

javascript - 在 Windows7 上安装 SRA-SiliconValley Jalangi 失败

node.js - Sequelize - 如果 allowNull === false 且值为 null 则运行验证

mysql - (Ruby) 如何转储数据库?

PHP/MySQL : How to verify properly escaped data?

c++ - X DevAPI mysqlx::Session() over linux socket 失败并显示 “CDK Error: unexpected message”

node.js - Sequelize CLI : cannot read property 'replace' of undefined when migrating DB