Skip to content

Documentation example creating with associations doesn't work #6882

Closed

Description

@mistertjen

What you are doing?

const Sequelize = require('sequelize')
const sequelize = new Sequelize('blogapplication', null, null, {
  dialect: 'postgres',
})

var Product = sequelize.define('product', {
  title: Sequelize.STRING
});
var User = sequelize.define('user', {
  first_name: Sequelize.STRING,
  last_name: Sequelize.STRING
});

Product.belongsTo(User);
// Also works for `hasOne`

sequelize.sync({force:true})
.then(x => {
  return Product.create({
    title: 'Chair',
    User: {
      first_name: 'Mick',
      last_name: 'Broadstone'
    }
  }, {
    include: [ User ]
  });
})

What do you expect to happen?

I tried to do creating with associations as is specified in the Sequelize documentation, this is the example code from the doc, the only difference is that I don't use this.sequelize.define, but use sequelize.define instead (I got a error with this and wasn't sure what to bind it to).

What is actually happening?

No creation of the associations (User) is done.

SQL output:
Executing (default): INSERT INTO "products" ("id","title","createdAt","updatedAt") VALUES (DEFAULT,'Chair','2016-11-18 06:43:43.662 +00:00','2016-11-18 06:43:43.662 +00:00') RETURNING *;

How it does work

This code does work when I write user exactly as it was named in sequelize.define, see working code below. Not sure if this is behavior is because I didn't use this.sequelize or a mistake in the documentation.

const Sequelize = require('sequelize')
const sequelize = new Sequelize('blogapplication', null, null, {
  dialect: 'postgres',
})

var Product = sequelize.define('product', {
  title: Sequelize.STRING
});
var User = sequelize.define('user', {
  first_name: Sequelize.STRING,
  last_name: Sequelize.STRING
});

Product.belongsTo(User);
// Also works for `hasOne`

sequelize.sync({force:true})
.then(x => {
  return Product.create({
    title: 'Chair',
    user: {
      first_name: 'Mick',
      last_name: 'Broadstone'
    }
  }, {
    include: [ User ]
  });
})

SQL output:
Executing (default): INSERT INTO "users" ("id","first_name","last_name","createdAt","updatedAt") VALUES (DEFAULT,'Mick','Broadstone','2016-11-18 06:52:11.822 +00:00','2016-11-18 06:52:11.822 +00:00') RETURNING *;
Executing (default): INSERT INTO "products" ("id","title","createdAt","updatedAt","userId") VALUES (DEFAULT,'Chair','2016-11-18 06:52:11.818 +00:00','2016-11-18 06:52:11.818 +00:00',1) RETURNING *;

__Dialect: postgres
__Database version: 6.1.0 (pg node module)
__Sequelize version: 3.24.8

Activity

  1. added
    docs
    on Nov 18, 2016
  2. janmeier commented on Nov 18, 2016

    @janmeier
    Member

    The name of the attribute (User) has to match the name of the association (user)

    So it's a bug in the documentation :) - PR welcome

  3. added a commit that references this issue on Nov 18, 2016
  4. mistertjen commented on Nov 18, 2016

    @mistertjen
    Author

    Cool, made the PR, thanks for the quick reply and merge!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions