【问题标题】:Sequelize - Foreign key that references same table primary keySequelize - 引用同一个表主键的外键
【发布时间】:2018-06-02 10:37:08
【问题描述】:

是否可以在 Sequelize 模型语法中构造以下内容?您想要嵌套数据的用途。

CREATE TABLE Data
(
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    ...
    `parentId` INT,
    FOREIGN KEY(parentId) REFERENCES Data(id)
);

编辑:是的

const Data = sequelize.define('Data', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
})

Data.belongsTo(Data, { foreignKey: 'id' });

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

    看来你可以!

    const Data = sequelize.define('Data', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
    })
    
    Data.belongsTo(Data, { foreignKey: 'id' });
    

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2018-03-25
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多