gpt4 book ai didi

mysql - 连接.sync({强制: false}) creates unwanted table

转载 作者:行者123 更新时间:2023-11-29 10:05:03 26 4
gpt4 key购买 nike

我正在使用我在“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 |
+--------------------+

请注意,我已将表名称从“user”更改为“Users”,而不是“users”,但创建的表是“users”。

尽管如此,我删除了表“users”,但如果我运行代码,它会重新创建。

我到处都看过,似乎没有任何对“用户”表或模型的引用。阻止创建表的唯一原因是从 index.js 中删除以下行:

connection.sync({force: false});

为什么会发生这种情况?如何防止创建表,即使 connection.sync 仍然存在?

最佳答案

我认为这可以帮助你:

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

关于mysql - 连接.sync({强制: false}) creates unwanted table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com