Sequelize Joining tables not created when using single create call with embedded object #13498
-
I am new to Sequelize and trying to follow their example for creating, in my case, a User that has many Roles. The following code snippet works as expected with my configuration. This creates the user and creates the UserRole table. const adminRole = await Role.create( {role: 'admin'} );
const basicRole = await Role.create( {role: 'basic'} );
const user = await User.create( userData, options );
user.addRoles([adminRole, basicRole]) However, I can't seem to get the single create a call to work. I would like to do this so I don't have to query the database to get the role objects when I will know the FK ahead of time. Forgive the code formatting, it is to conserve space in the question. // define the role table
class Role extends Model{}
Role.init( { role: { primaryKey: true, type: Sequelize.DataTypes.TEXT() } },
{ sequelize, modelName: 'Role' }
);
// define the user table
class User extends Model{}
User.init({
id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.UUIDV4,
allowNull:false, primaryKey: true, },
nameUser: { type: Sequelize.DataTypes.TEXT(), allowNull:false }},
{ sequelize, modelName: 'User' });
// define the joining table
class UserRole extends Model {}
UserRole.init({}, { sequelize: db.db, modelName: 'UserRole' }
// Link the table via the UserRole table
Role.belongsToMany( User, {through: UserRole} );
User.belongsToMany( Role, {through: UserRole} ); At some stage in the lifecycle of the app Roles have been added to the database const admin = await Role.create({role: 'admin'}); At some point in the future when a new user joins on in my route handler.... const userData = { nameUser: 'test-name-2', roles:[{role:'admin'}] };
const options = { include: Role };
// No join table is created.
const user = await User.create( userData, options ); I have tried so many variations of objects and key names. But I can't seem to recreate the behavior that is achieved through the
What am I doing wrong, or what do I not understand? I appreciate any guidance and thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I got the correct combination working and added some better error logging. const userData = { nameUser: 'test-name-2', Roles:[ {role:'admin'} ] };
const options = { include: Role };
// No join table is created.
const user = await User.create( userData, options ); So it looks like the part I didn't understand was that the create call wasn't going to create my UserRole table but instead create another Role entry. As the Role already existed, I was getting an error on must be unique. I am not sure if it is even possible to create the user table automatically when creating the user entry. |
Beta Was this translation helpful? Give feedback.
I got the correct combination working and added some better error logging.
So it looks like the part I didn't understand was that the create call wasn't going to create my UserRole table but instead create another Role entry. As the Role already existed, I was getting an error on must be unique. I am not sure if it is even possible to create the user table automatically when creating the user entry.