K..
K..

Reputation: 4223

Named associations with sequelize.js

This seems to create one column roomId

Room.hasMany(Room.);
Room.belongsTo(Room);

This creates the columns parentId and roomId

Room.hasMany(Room);
Room.belongsTo(Room, {as:'parent'});

This throws an error:

Room.hasMany(Room, {foreignKey:'parent'});
Room.belongsTo(Room, {as:'parent'});

I expected it to work like the first example, but with different column title.

Background:

Since it is a simple one-to-many association I only want one column.

I want to query with Room.findAll({where:{parentId:123}}) and use getParent()

Upvotes: 2

Views: 341

Answers (1)

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28778

Room.hasMany(Room, { foreignKey: 'parentId' );
Room.belongsTo(Room, { foreignKey: 'parentId', as:'parent' });

Upvotes: 3

Related Questions