【问题标题】:How to reference two tables using hasOne with sequelize.js如何使用 hasOne 和 sequelize.js 引用两个表
【发布时间】:2016-05-06 23:21:18
【问题描述】:

考虑 sequelize-auto 生成的这 3 个模型:

sequelize.define('users', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        first: {
            type: DataTypes.STRING,
            allowNull: true
        },
        last: {
            type: DataTypes.STRING,
            allowNull: true
        }
    }, {
        tableName: 'users',
        underscored: true,
        timestamps: false
    });

sequelize.define('groups', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        parent_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'groups',
                key: 'id'
            }
        }
    }, {
        tableName: 'groups',
        underscored: true,
        timestamps: false
});

sequelize.define('user_groups', {
        group_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            references: {
                model: 'groups',
                key: 'id'
            }
        },
        user_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'user_groups',
        underscored: true,
        timestamps: false
    });

我期待生成 hasOne 语句,但我必须像这样指定它们:

user_groups.hasOne(orm.models.users, { foreignKey: "id" });
user_groups.hasOne(orm.models.groups, { foreignKey: "id" });

还要考虑表格中的以下数据:

users (id, first, last):
1, John, Doe

group (id, parent_id):
1, NULL
2, NULL
3, NULL
4, NULL

user_groups (group_id, user_id):
1, 1
4, 1

执行此查询:

sequelize.findAll("user_groups", {
    attributes: ["*"],
    raw: true,
    include: [{
        model: models.users,
    }]
});

我得到以下结果:

[ { group_id: 4,
    user_id: 1,
    'user.id': null,
    'user.first': null,
    'user.last': null },
  { group_id: 1,
    user_id: 1,
    'user.id': 1,
    'user.first': 'John',
    'user.last': 'Doe' } ]

这清楚地表明 sequelize 正在使用 group_id 作为 user_id 关系。

如何指定将 user_groups 关系链接到其各自表的关系,以便能够将用户与多个组相关联?

我也很好奇模型定义中的“引用”键应该如何工作,因为文档不存在。

【问题讨论】:

    标签: javascript node.js postgresql sequelize.js


    【解决方案1】:

    我能够使用这些关联获取引用的数据:

    groups.hasMany(orm.models.user_groups);
    user_groups.belongsTo(orm.models.groups, {foreignKey: "group_id", as: "group"});
    
    users.hasMany(orm.models.user_groups);
    user_groups.belongsTo(orm.models.users, {foreignKey: "user_id", as: "user"});
    

    还有以下查询:

    sequelize.findAll("user_groups", {
        attributes: ["*"],
        raw: true,
        include: [
            { model: users, foreignKey: "user_id", as: "user", attributes: ["first", "last"] }
        ]
    });
    

    预期结果:

    [ { group_id: 4,
        user_id: 1,
        'user.first': 'John',
        'user.last': 'Doe' },
      { group_id: 1,
        user_id: 1,
        'user.first': 'John',
        'user.last': 'Doe' } ]
    

    【讨论】:

    • 你怎么会卡住,以至于你问了一个问题却在 18 分钟后自己回答了这个问题?此外,文档中有大量的参考示例。 docs.sequelizejs.com/en/latest/docs/associations
    • 写下这个问题并查看类似的问题让我非常专注于我遇到的问题,因此我很快找到了答案。它经常发生在我身上。文档仍然没有解释模型中的引用子句。如果有,请指点我。
    • 查看 'define' 方法下的表格以获取参考说明。 docs.sequelizejs.com/en/latest/api/sequelize
    • 感谢您向我指出,我试图在文档的模型部分找到它。它似乎定义了外键以及 CASCADE 等等。它与设置 hasOne、belongsTo 等关联有什么关系吗?也许这是堆栈交换的另一个问题。我可能会在 18 分钟内弄清楚。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多