gpt4 book ai didi

sequelize.js - Sequelize-CLI 向现有模型添加列

转载 作者:行者123 更新时间:2023-12-04 05:01:18 24 4
gpt4 key购买 nike

我一直在阅读大量的 sequelize-cli 文档,但似乎无法弄清楚如何向我现有的模型添加一列。我有一个模型叫,models/team.js并想添加一个名为 role_name 的列到模型 sequelize model:create --name team --attributes role_name:string ,但我收到一条消息要覆盖而不是修改:

Loaded configuration file "config/config.json".
Using environment "development".
The file /Users/user/Desktop/Projects/node/app-repo/app/models/team.js already exists. Run "sequelize model:create --force" to overwrite it.

我不想覆盖让我认为这不是正确使用命令的文件。这也让我想知道这是否无法从 cli 中实现,并且必须在迁移文件级别进行调整。

最佳答案

您需要另一个迁移文件来添加列。

在 up 函数中,您需要添加

 queryInterface.addColumn(
'nameOfAnExistingTable',
'nameofTheNewAttribute',
Sequelize.STRING)

在向下功能中,您需要
queryInterface.removeColumn(
'nameOfAnExistingTable',
'nameOfTheAttribute')

然后运行迁移。

关于sequelize.js - Sequelize-CLI 向现有模型添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009062/

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