Sequelize - 引用同一个表主键的外键 [英] Sequelize - Foreign key that references same table primary key

查看:60
本文介绍了Sequelize - 引用同一个表主键的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to construct the following in Sequelize model syntax? The use being you want to have nested data.

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' });

推荐答案

看来你可以!

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

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

这篇关于Sequelize - 引用同一个表主键的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆