【问题标题】:sequilize Referencing column 'partner_id' and referenced column 'id' in foreign key constraint 'partnerserviceareas_ibfk_1' are incompatible外键约束“partnerserviceareas_ibfk_1”中的引用列“partner_id”和引用列“id”不兼容
【发布时间】:2021-12-27 12:39:10
【问题描述】:

这是我的合作伙伴模型

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require('../connection');

const PartnerInfo = sequelize.define(
  'partnerinfo',
  {
    id: {
      type: DataTypes.UUID,
      allowNull: false,
      defaultValue: Sequelize.UUIDV4,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    phone: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    loginSecret: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    gender: {
      type: DataTypes.INTEGER,
      defaultValue: 0, // 0:Female 1:male
    },
    dob: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    image: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    type: {
      type: DataTypes.INTEGER,
      defaultValue: 0, // 0:Value 1:Elite
    },
    joiningDate: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    termination_date: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    line1: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    line2: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    landmark: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    city: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    state: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    country: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    pincode: {
      type: DataTypes.STRING,
      allowNull: true,
    },
  },
  {
    timestamps: true,
  }
);

module.exports = PartnerInfo;

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require('../connection');
const PartnerInfo = require('./partnerinfo');
const ServiceArea = require('./servicearea');

const PartnerServiceArea = sequelize.define('partnerservicearea', {
  id: {
    type: DataTypes.UUID,
    allowNull: false,
    defaultValue: Sequelize.UUIDV4,
    primaryKey: true,
  },
  partner_id: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'partnerinfos',
      key: 'id',
    },
  },
  servicearea_id: {
    type: DataTypes.CHAR,
    allowNull: false,
  },
});

PartnerServiceArea.belongsTo(PartnerInfo, {
  foreignKey: {
    name: 'partner_id',
    allowNull: false,
  },
  targetKey: 'id',
});

PartnerInfo.hasMany(PartnerServiceArea, {
  foreignKey: {
    name: 'partner_id',
    allowNull: false,
  },
  targetKey: 'id',
  onDelete: 'CASCADE',
  onUpdate: 'CASCADE',
});

PartnerServiceArea.belongsTo(ServiceArea, {
  foreignKey: {
    name: 'servicearea_id',
    allowNull: false,
  },
  targetKey: 'id',
  onDelete: 'CASCADE',
  onUpdate: 'CASCADE',
});

ServiceArea.hasMany(PartnerServiceArea, {
  foreignKey: {
    name: 'servicearea_id',
    allowNull: false,
  },
  targetKey: 'id',
  onDelete: 'CASCADE',
  onUpdate: 'CASCADE',
});

module.exports = PartnerServiceArea;

这是我尝试关联并使用默认同步的两个模型。

我需要链接方面的帮助并想使用复杂的查询

这是 SQL 查询

CREATE TABLE IF NOT EXISTS `partnerserviceareas` 
(
     `id` CHAR(36) BINARY NOT NULL, 
     `partner_id` CHAR(36) BINARY NOT NULL, 
     `servicearea_id` CHAR(255) NOT NULL, 
     `createdAt` DATETIME NOT NULL, 
     `updatedAt` DATETIME NOT NULL, 

     PRIMARY KEY (`id`), 
     FOREIGN KEY (`partner_id`) REFERENCES `partnerinfos` (`id`) 
             ON DELETE NO ACTION ON UPDATE CASCADE, 
     FOREIGN KEY (`servicearea_id`) REFERENCES `service_areas` (`id`) 
             ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

错误是

外键约束“partnerserviceareas_ibfk_1”中的引用列“partner_id”和引用列“id”不兼容

【问题讨论】:

  • 同时显示partnerinfos表的DDL

标签: javascript mysql sequelize.js


【解决方案1】:

如果数据类型为“string/char”类型,请检查两个相关字段的“charter set”和“collat​​ion”。

整数数据类型上不存在这样的问题(“外键约束'xxx'不兼容”)(或者至少我没有遇到这样的不兼容问题)。

有时创建的表与外部表(由其他人创建的外部表,从旧转储恢复等)具有不同的“章程集/排序规则”。

如果创建新表(charter/collat​​ion 与外表相同):

CREATE TABLE IF NOT EXISTS `partnerserviceareas`  (
    ...
)
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

如果您已经拥有两个表并想更改列:

ALTER TABLE `partnerserviceareas` 
CHANGE COLUMN `partner_id` `partner_id` CHAR(36) 
CHARACTER SET 'utf8mb4'
COLLATE 'utf8mb4_unicode_ci' NOT NULL ;

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 2021-06-01
    • 2020-08-01
    • 2019-06-23
    • 1970-01-01
    • 2020-12-14
    • 2021-06-12
    • 2021-08-31
    相关资源
    最近更新 更多