Mastertrap
Mastertrap

Reputation: 63

Change name of table when retrieved it

I have a sequelize model like this:

var User_Exercises = sequelize.define('user_exercises', {
 id: {
    type: Sequelize.BIGINT(11),
    autoIncrement: true,
    allowNull: false,
    primaryKey: true
},
.
.
.

This exercise model belongs to another model (Sets). So when i retrieve sets I include the exercise. The problem is that in the JSON ( Every Set has one or more exercises) I want that 'user exercises' to be renamed just 'exercises'. Is there a way to give it an alias or something when i query the database?

Workouts.findAll({where: {user_fkey: curentUser}, include: [{model: 
Sets, include: [{model:UserExercises}]}]}).then((workouts)=>{
        res.send(workouts);
    })  

I can't rename the table "exercises" because I already have another table named 'exercises'. but in my app I use the same class to pas these JSONS and all exercises has to be named 'exercises'.

Thank you and have a nice day!

I can't rename the table. I've searched on google for some time but i didn't find a solution.

Upvotes: 0

Views: 352

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58593

You can do that by assign alias to association :

Workouts.hasMany(UserExercises ,{ as: 'exercises' });

In include use it like :

include: [
    {
        model:UserExercises,
        as : 'exercises'
    }
]

Upvotes: 1

Related Questions