English 中文(简体)
How to make Sequelize use singular table names
原标题:

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? Thanks.

问题回答

The docs state that you can use the property freezeTableName.

Please take a look at this example:

var Bar = sequelize.define( Bar , { /* bla */ }, {
  // don t add the timestamp attributes (updatedAt, createdAt)
  timestamps: false,

  // don t delete database entries but set the newly added attribute deletedAt
  // to the current date (when deletion was done). paranoid will only work if
  // timestamps are enabled
  paranoid: true,

  // don t use camelcase for automatically added attributes but underscore style
  // so updatedAt will be updated_at
  underscored: true,

  // disable the modification of tablenames; By default, sequelize will automatically
  // transform all passed model names (first parameter of define) into plural.
  // if you don t want that, set the following
  freezeTableName: true,

  // define the table s name
  tableName:  my_very_custom_table_name 
})

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
})

You can Do it direct rather than specifying in every table you have define it once like below

var db_instance = new Sequelize(config.DB.database, config.DB.username, config.DB.password, {
  host: config.DB.host,
  dialect: config.DB.dialect,
  define: {
    timestamps: true,
    freezeTableName: true
  },
  logging: false
});  

OR

You can simply tell Sequelize the name of the table directly as well:

sequelize.define( User , {
  // ... (attributes)
}, {
  tableName:  Employees 
});

You can see both Method in Documentation of sequelize.js

Doc. Of sequelize.js related to freezeTableName

If you require to have different model names for singuar and plural definitions you can pass name as a parameter in options of model.

Please take a look at this example:

    const People = sequelize.define( people , {
    name: DataTypes.STRING,
}, {
    hooks: {
        beforeCount (options) {
            options.raw = true;
        }
    },
    tableName:  people ,
    name: {
        singular:  person ,
        plural:  people 
    }
});

this will return "person" as an object when a single record is being queried and "people" as an array when we fetch multiple records.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签