Skip to content

Creating with associations, docs need clarification #9103

Closed

Description

@pzanitti

What are you doing?

A Person has many Emails, Phones, Addresses. An Address has a City. A City has many Addresses.

db.Person.Emails = db.Person.hasMany(db.Email)
db.Person.hasMany(db.Phone)
db.Person.hasMany(db.Address)
db.Address.belongsTo(db.City)

In a single create I want to populate all those tables. Now after a couple hours of hammering away I got it to work, but I don't know why.

"personData": {
	"name": "foo",
	"Emails": [{
		"email": "[email protected]"		
	}, {
		"email": "[email protected]"
	}],
	"Phones": [{
		"phone": 123123123
	}],
	"Addresses": [{
		"address": "ayy 123",
		"City": {
			"name": "Wew",
			"code": 1234
		}
	}]
}

model.Person.create(personData, {
        include: [ model.Email, model.Phone, {
          model: model.Address,
          include: [ model.City ]
        } ]
      })

I got the idea from the Creating elements of a "HasMany" or "BelongsToMany" association section, because it uses a model as argument for include, and the bottom one does { model: model, ... arguments} so I figured I could place a nested include there.

Yet the other examples use association, one example even saves the instance of the associations to refer to them, with little explanation.

Honestly I find it overly confusing since it seems I got it to work fine going against most everything the docs say.

Dialect: n/d
Dialect version: n/d
Database version: n/d
Sequelize version: 4.33.4
Tested with latest release: Yes, 4.33.4

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. added
    docs
    on Apr 7, 2018
  2. deleted a comment from stale on Jul 30, 2019
  3. self-assigned this
    on Jul 30, 2019
  4. added
    status: understood
    on Jul 30, 2019
  5. added
    status: wip
    and removed
    status: understood
    on Sep 6, 2019
  6. removed
    status: wip
    on Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions