簡體   English   中英

如何使用hasOne和sequelize.js引用兩個表

[英]How to reference two tables using hasOne with sequelize.js

考慮到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關系鏈接到它們各自的表的關系,以便能夠將用戶與多個組相關聯?

我也很好奇,因為該文檔不存在,模型定義中的“引用”鍵應該如何工作。

我能夠使用以下關聯獲取引用的數據:

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' } ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM