connection.sync({force:false})创建不需要的表

问题描述 投票:0回答:1

我正在使用我在'Medium'上找到的代码并更改了我的模型中表格的名称,形式如下:

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;

至:

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;

我还更改了数据库中表的名称。

然后我决定回到原来的名字。然而,从那时起,每次运行整个代码时,我的数据库都会显示2个表:

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

请注意,我已将表名从“用户”更改为“用户”而不是“用户”,但创建的表是“用户”。

尽管如此,我删除了表'用户',但如果我运行代码,它将被重新创建。

我到处寻找它似乎没有任何“用户”表或模型的引用。唯一阻止创建表的方法是从index.js中删除以下行:

connection.sync({force: false});

为什么会发生这种情况,如何阻止创建表,甚至连接.sync仍然存在?

mysql node.js sequelize.js
1个回答
1
投票

我想这可以帮到你:

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

更多细节:DO READ

© www.soinside.com 2019 - 2024. All rights reserved.