Sails.js How to insert data into a join table (Many to Many)

user2663041

I am having an error inserting data into my join table and I don't know if I'm doing it the right way. Here are my 2 models that have the many to many association.

Commit.js:

module.exports = {
  schema: true,
  attributes: {
    idCommit : {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true,
      unique: true
    },
    revision : {
    type: 'integer',
    required: true
    },
    issues: {
      collection: 'issue',
      via: 'commits',
      dominant: true
    }
  }
};

Issue.js:

module.exports = {
  schema: true,
  attributes: {
    idIssue : {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true,
      unique: true
    },
    description: {
    type: 'string',
    required: true
    },
    commits: {
      collection: 'commit',
      via: 'issues'
    }
  }
};

When I try to insert issues into a commit this way :

Commit.create(commit).exec(function(err,created){
  if (err) {
    console.log(err);
  }
  else { 
    created.issues.add(issues);
    created.save(function(err) {});
  }
});

My commit is created, my issues are created, but there is no link what so ever between them and the join table stays empty. Where did I get it wrong?

Jason Kulatunga

From your code it looks like your trying to add an array of issues, try doing the association individually, like they do in the Many-to-Many docs

Commit.create(commit).exec(function(err,created){
  if (err) {
    console.log(err);
  }
  else { 

    issues.forEach(function(issue, index){
        created.issues.add(issue);
    })

    created.save(function(err) {});
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert into Many to Many Table - Node JS MySQL

From Dev

How to insert data in many to many relationship

From Dev

How to insert constant value in a join table with Hibernate in a many-to-many relation?

From Dev

How to concatenate data from a many to many table?

From Dev

How to sort many to many through a join model data with Ember.js and Ember Data

From Dev

Many to Many relation with join table

From Dev

Core Data Join Table Many to Many Relationship - Object Creation

From Dev

Rails 4: get data from many to many join table

From Dev

Sequelize.JS many-to-many findAll not using join table

From Dev

sails.js many to many query

From Dev

Bidirectional Many to many association on Sails.js

From Dev

Sails JS complex many to many association

From Dev

sails.js many to many query

From Dev

How can I make a Many to many relation on same table (many to many Join To Self)

From Dev

MySQL - How to insert into table that has many-to-many relationship

From Dev

How to insert into associative table in many to many relationship by Hibernate?

From Dev

How to join one to many distinct in three table?

From Dev

Insert into table many values

From Dev

How to join many to many in createQuery()

From Dev

How To Loop SQL Insert for many data On PHP

From Dev

Sails many to many association

From Dev

collection_check_boxes in nested forms won't insert into join table correctly with many to many

From Dev

SQL join on junction table with many to many relation

From Dev

Many to many association without join table

From Dev

Sequelize not creating join table many-to-many

From Dev

Hibernate many-to-many join table not populating

From Dev

Doctrine many-to-many join table not populating

From Dev

Rails: many to many relationship join table design

From Dev

foreignKey or inverseForeignKey for many-to-many join table

Related Related

  1. 1

    Insert into Many to Many Table - Node JS MySQL

  2. 2

    How to insert data in many to many relationship

  3. 3

    How to insert constant value in a join table with Hibernate in a many-to-many relation?

  4. 4

    How to concatenate data from a many to many table?

  5. 5

    How to sort many to many through a join model data with Ember.js and Ember Data

  6. 6

    Many to Many relation with join table

  7. 7

    Core Data Join Table Many to Many Relationship - Object Creation

  8. 8

    Rails 4: get data from many to many join table

  9. 9

    Sequelize.JS many-to-many findAll not using join table

  10. 10

    sails.js many to many query

  11. 11

    Bidirectional Many to many association on Sails.js

  12. 12

    Sails JS complex many to many association

  13. 13

    sails.js many to many query

  14. 14

    How can I make a Many to many relation on same table (many to many Join To Self)

  15. 15

    MySQL - How to insert into table that has many-to-many relationship

  16. 16

    How to insert into associative table in many to many relationship by Hibernate?

  17. 17

    How to join one to many distinct in three table?

  18. 18

    Insert into table many values

  19. 19

    How to join many to many in createQuery()

  20. 20

    How To Loop SQL Insert for many data On PHP

  21. 21

    Sails many to many association

  22. 22

    collection_check_boxes in nested forms won't insert into join table correctly with many to many

  23. 23

    SQL join on junction table with many to many relation

  24. 24

    Many to many association without join table

  25. 25

    Sequelize not creating join table many-to-many

  26. 26

    Hibernate many-to-many join table not populating

  27. 27

    Doctrine many-to-many join table not populating

  28. 28

    Rails: many to many relationship join table design

  29. 29

    foreignKey or inverseForeignKey for many-to-many join table

HotTag

Archive