Skip to content

How to insert a new record with relations? #162

Closed

Description

@deleteman

I have ModelA that hasMany ModelB elements, how do I insert a new modelA with the associations already there for modelB? Or how do I associate the new record with the records of ModelB after the first one is saved?

Thanks!

Activity

  1. rainulf commented on May 17, 2013

    @rainulf

    It's not documented properly, but it's there. Whenever you do an association, accessors such as get, set, has, and del are added.

    var Person = db.define('person', {
        name : String
    });
    var Animal = db.define('animal', {
        name : String
    });
    Animal.hasOne("owner", Person); // assumes column 'owner_id' in 'animal' table
    
    // get animal with id = 123
    Animal.get(123, function (err, Foo) {
        // Foo has getOwner(), setOwner(), hasOwner(), etc.
    });

    You can check the source here; usage in the test files here.

  2. dxg commented on May 19, 2013

    @dxg
    Collaborator

    I'm starting to think we need a wiki..

  3. gaudecker commented on May 21, 2013

    @gaudecker

    Did I misunderstand the original question? Is there a way to set the associations when calling create on a model?

    Animal.create([{
      name: 'kittycat',
      owner: personInstance
    }], ...

    Of course the example given by @rainulf gave works, but I think it'd be much cleaner this way.

  4. dresende commented on May 24, 2013

    @dresende
    Owner

    There's no option to create several instances of different models in one call, but you can create the instances manually and when you call .save() on the top one it should propagate.

    var Person = db.define('person', {
        name : String
    });
    var Animal = db.define('animal', {
        name : String
    });
    Animal.hasOne("owner", Person);
    
    // ...
    
    var John = new Person({ name: "John" });
    var Deco = new Animal({ name: "Deco" });
    Deco.setOwner(John, function () {
        // John should be saved (to fetch it's ID) and also Deco should be saved
    });
  5. dresende commented on May 31, 2013

    @dresende
    Owner

    I'm going to try this in a couple of minutes. If I'm successful I'll post an example.

  6. dresende commented on Jun 2, 2013

    @dresende
    Owner

    You should now be able to do something like:

    var John = new Person({
        name: "John",
        surname: "Doe",
        pets: [ new Pet({ name: "Deco" }), ... ]
    });
    John.save(function (err) {
        // assuming Person.hasMany("pets", Pet), Deco should be saved, so as John
    });
  7. ptnplanet commented on Jun 2, 2013

    @ptnplanet

    Awesome!

  8. dresende commented on Jun 4, 2013

    @dresende
    Owner

    I'm going to close this for now. I'll add some tests a few tiny tweaks in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions