Sequelize Solution Model is not an instance created by sequelize

Recent research on Express framework, and then to Yeoman It picks up an Express project to play with, based on MVC ideas.

It took me a long time to see data access there, so I spent a little time studying it. Sequelize This is an ORM for nodejs. Before that, I was familiar with Microsoft's father's Microsoft Entity Framework, or climbed a little pit.

sequelize is the CodeFirst development model (Entity Framework and DbFirst model), so the most critical thing is to solve the problem of the model.

After some exploration, the following conclusions are drawn:

ORM is absolutely confident. ID is the primary key, so you should not add some strange fields as the primary key. (The same is true of the Entity Framework)

2. For foreign keys, you don't need to manually create foreign key fields, just tell Sequelize, it will help you create.

3. The tables created by Sequelize will have the fields of "createdAt" and "uodatedAt" by default, which can be manually configured to close.

Following is a code example of a model I configured:

module.exports = function (sequelize, DataTypes) {

  var Student = sequelize.define('Student', {
    id:{
        type:DataTypes.INTEGER,
        primaryKey:true,
        autoIncrement:true
    },
    name:DataTypes.STRING(50),
    age:DataTypes.INTEGER,
    isdel:DataTypes.BOOLEAN
  }, {
    'timestamps': false,
    "createdAt":false,
    "updatedAt":false,
    "deletedAt":false,
    classMethods: {
      associate: function (models) {
        // example on how to add relations
        // Article.hasMany(models.Comments);
        //Foreign key relationship model
      }
    }
  });

  return Student;
};
One problem that needs to be noticed is that when this model is exported, it needs to receive parameters. When constructing foreign key relations, because of negligence, it climbs a pit for a long time. If there are no parameters, the system will prompt:

The model is not an instance created by sequelize
Add foreign key relationships (Note that such tables cannot exist in the database beforehand and can be deleted with {force:true}, otherwise filling in the relationship will fail):

module.exports = function (sequelize, DataTypes) {

  var Student=require("./student.js")(sequelize, DataTypes);
  //Pay attention to keeping up with the parameters
  var Book=require("./books.js")(sequelize, DataTypes);
  //Pay attention to keeping up with the parameters
  var StudentToBook = sequelize.define('StudentToBook', {
    id:{
        type:DataTypes.INTEGER,
        primaryKey:true,
        autoIncrement:true
    },
    // sid:{
    //     type:DataTypes.INTEGER,
    //     allowNull:false
    // },
    // bid:{
    //     type:DataTypes.INTEGER,
    //     allowNull:false
    // },
    isdel:DataTypes.BOOLEAN
  }, {
    'timestamps': false,
    "createdAt":false,
    "updatedAt":false,
    "deletedAt":false
  });


    //Adding foreign key relationships
    Book.belongsToMany(Student,{'through':StudentToBook});

    //Adding foreign key relationships
    Student.belongsToMany(Book,{"through":StudentToBook});


  return StudentToBook;
};

In this way, the problem can be solved. Run node app.js, open navicat, and you can see that the data table has been created successfully.


Methods can be defined on Sequelize's model, which can perform some convenient data manipulation, and are interested in referring to official documents. At the same time, I wonder if it is possible to directly encapsulate a data access layer like ASP.NET MVC.

Finally, end with the line of "Three Kingdoms Kill Zhongsun Boss": "Wait a minute, let me think twice".

Reference: https://segmentfault.com/a/1190000003987871#articleHeader23


Tags: Database Navicat

Posted on Tue, 16 Apr 2019 23:24:32 -0400 by JChilds