Noah Goodrich
Noah Goodrich

Reputation: 25263

How do you correctly access the defined models from a sequelize instance?

I've seen this syntax to access a defined model from the Sequelize instance:

var sequelize = new Sequelize('database', 'username'[, 'password']);

var Project = sequelize.define('Project', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT
});

sequelize.Project.build({});

However, when I tried it myself on 1.7.0:

console.log(sequelize.Project);

Returned undefined

Is there another way or a correct way to accomplish this?

Upvotes: 5

Views: 4760

Answers (3)

Crusader
Crusader

Reputation: 1200

I use this way: sequelize.models.Project

Upvotes: 2

Noah Goodrich
Noah Goodrich

Reputation: 25263

You need to attach the object on your own. Usually people do something like:

sequelize.Project = sequelize.import('./models/project');

Sequelize doesn't do that for you. (The fact that people decide to attach models to the sequelize object is purely a convenience thing that emerged)

Source: Github Issues #1693

Upvotes: 2

shangsunset
shangsunset

Reputation: 1615

you need to save it afterwards and catch the events in callbacks. something like:

Project.save().success(function(err, project) {
     console.log(project)

    })

or:

Project
      .create({})
      .complete(function(err,project){

      console.log(project)
})

Upvotes: -1

Related Questions