Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] ManyToMany relationship saving data in merge table #756

Open
galalaly opened this issue Nov 24, 2016 · 0 comments
Open

[Question] ManyToMany relationship saving data in merge table #756

galalaly opened this issue Nov 24, 2016 · 0 comments

Comments

@galalaly
Copy link

galalaly commented Nov 24, 2016

I have a User Model with Many to many relationship with an Activity model.

The joint/merge table contains extra data. When I run the following code, only the user_id and the activity_id are inserted in the joint table.

user.activities = myActivities;
user.save();

Model looks like

User.hasMany( 
        'activities', // realtionship name on the model
        Activity, // the related model
        { 
            share: Number
        }, // join table extra fields
        {
            mergeId: 'user_id', // Custom name for column referencing this model
            mergeAssocId: 'activity_id', // Custom name for column referencing other model,
            reverse: 'users' // define the same relationship in the other model as well with this name (getUsers() from the other side)
        }
    );

The myActivities looks like

myActivities = [{ 'activity_id' : 1, 'activity_name' : 'like', 'share' : 1 }];

Any clue what's happening and how can I alter this? I prefer to have this so that I hit the database supposedly once for all of the myActivities elements instead of looping on each of the activities and using the setActivities function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant