简体   繁体   中英

How to reference two tables using hasOne with sequelize.js

Considering these 3 models generated by sequelize-auto:

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

I was expecting hasOne statements to be generated but I had to specify them like so:

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

Also consider the following data in tables:

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

Doing this query:

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

I get the following results:

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

This clearly shows that sequelize is using group_id for the user_id relation.

How can I specify a relation that links the user_groups relations to their respective tables in order to be able to associate a user to many groups?

I am also very curious as how the "references" key in the models definition is supposed to work as the documentation is inexistant on that.

I was able to get the referenced data using these associations:

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

And the following query:

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

With the expected results:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM