#Associations in sequelize

12 messages · Page 1 of 1 (latest)

sleek pond
#

Hi everyone!

Basicaly im trying make a association between two tables but i never used sequelize to do that, so it's my first time trying but when i do that here

...
import TaskModel from "./task.model";

interface UserCreationAttributes extends Optional<UserInterface, 'userId' | 'tasksConcluded'> {}

class UserModel extends Model<UserInterface, UserCreationAttributes> implements UserInterface {
...
}

UserModel.init(
   ...
);

UserModel.hasMany(TaskModel, { foreignKey: 'ownerId' }); // <-- here

export default UserModel;

and here

...
import UserModel from "./user.model";

interface TaskCreationAttributes extends Optional<TaskInterface, 'taskId' | 'status'> {}

class TaskModel extends Model<TaskInterface, TaskCreationAttributes> implements TaskInterface {
  ...
}

TaskModel.init(
 ...
);

TaskModel.belongsTo(UserModel, { foreignKey: 'ownerId' }); <-- here too 

export default TaskModel;

i got this error message

2024-12-24T13:49:06.011-03:00 - error: uncaughtException: TaskModel.belongsTo called with something that's not a subclass of Sequelize.Model
Error: TaskModel.belongsTo called with something that's not a subclass of Sequelize.Model
indigo estuary
#

are you not using the sequelize annotation on your class attributes?

#

looks a bit odd calling the class static methods

#

oh, are you trying to use the legacy model definition?

dire lagoon
#

The error you're seeing, TaskModel.belongsTo called with something that's not a subclass of Sequelize.Model, means Sequelize expects UserModel to be set up correctly as a subclass of Sequelize.Model before you use .belongsTo.

sleek pond
#

!resolved

dire lagoon
#

contact me anytime when needed.

sleek pond
#

Thank you! I will keep this in mind and reach out if I need your help.

dire lagoon
#

could you accept my friend request?

#

It would be helpful if we stayed close to each other.