How to make Sequelize use singular table names

后端 未结 4 1746
无人及你
无人及你 2020-12-04 10:13

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names

4条回答
  •  感情败类
    2020-12-04 10:46

    While the accepted answer is correct, you can do this once for all tables rather than having to do it separately for each one. You simply pass in a similar options object into the Sequelize constructor, like so:

    var Sequelize = require('sequelize');
    
    //database wide options
    var opts = {
        define: {
            //prevent sequelize from pluralizing table names
            freezeTableName: true
        }
    }
    
    var sequelize = new Sequelize('mysql://root:123abc@localhost:3306/mydatabase', opts)
    

    Now when you define your entities, you don't have to specify freezeTableName: true:

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

提交回复
热议问题