Jim*_*m-Y 5 javascript node.js sequelize.js
我想在迁移脚本中使用续集模型。有没有可能,如果有,能否举个例子?谢谢
我正在创建一个表,帐户,在使用迁移脚本创建它之后,我想遍历所有未关联的用户(旧用户)(~还没有帐户)并为这些用户创建一个新帐户老用户。为此,我想使用续集模型来编写:User.findAll({ include: [Account], where: { Account: null } })我知道这有点太奇特了,我可以写一个续集声明来创建这些帐户,但是.. :D
当我尝试要求 sequelize 模型时,迁移总是会引发[SyntaxError: Unexpected token =]错误。请注意,在脚本创建表(帐户)后,我只需要模型(帐户)。我的模型文件中没有语法错误,因为否则它可以工作,但是当我尝试在迁移脚本中使用它时,它没有。
刚刚遇到这个问题,这只是需要模型并使用它们的问题。
'use strict';
const { User, Account } = require('../models'); // Assuming migrations is next to models
module.exports = {
up: async (queryInterface, Sequelize) => {
// Make the database migrations
await queryInterface.createTable('Accounts', .......)
await queryInterface.addColumn('Users', 'AccountId', {
type: Sequelize.INTEGER,
references: { model: 'Accounts', key: 'id' }
// ... more stuff here
})
// Fill up data
const usersWithoutAccounts = await User.findAll({ where: {AccountId: null}})
await Promise.all(usersWithoutAccounts.map(async user => {
const account = await Account.create({ props })
await user.update({ AccountId: account.id })
}))
},
down: async (queryInterface, Sequelize) => {
// ... undo changes
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1385 次 |
| 最近记录: |