Marcello
Marcello

Reputation: 173

connection.sync({ force: false}) creates unwanted table

I am playing with a code I have found on 'Medium' and changed the name of the tables in my models, form:

const connection = require ('../connection');
const { Sequelize } = connection;

const User = connection.define('user', {
    id: {
        type: connection.Sequelize.UUID,
        defaultValue: connection.Sequelize.UUIDV4,
        primaryKey: true
    },
    name: Sequelize.STRING,
    password: Sequelize.STRING

});

module.exports = User;

to:

const connection = require ('../connection');
const { Sequelize } = connection
const User = connection.define('Users', {
    id: {
        type: connection.Sequelize.UUID,
        defaultValue: connection.Sequelize.UUIDV4,
        primaryKey: true
    },
    name: Sequelize.STRING,
    password: Sequelize.STRING

});

module.exports = User;

I have also changed the name of the table in my database.

I have then decided to return to the original names. Since then however, every time I run the entire code, my database shows 2 tables:

+--------------------+
| Tables_in_database |
+--------------------+
| user               |
| users              |
+--------------------+ 

Note that I have changed the table name from 'user' to 'Users' and not to 'users', but the table created is 'users'.

Nevertheless, I dropped the table 'users' but if I run the code, it is re-created.

I have looked everywhere and it doesn't seems to be any reference to a 'users' table or model. the only thing that prevented the table to be created is to remove the following line from the index.js:

connection.sync({force: false});

Why is this happening and how can I prevent the table to be created and even with the connection.sync still in place?

Upvotes: 0

Views: 72

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58573

I think this can help you :

const Bar = sequelize.define('bar', { /* bla */ }, {
  // disable the modification of table names; 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',
})

For more detail : DO READ

Upvotes: 1

Related Questions