sequelize.sync({ force: true }) 有时无法正常工作 [英] sequelize.sync({ force: true }) is not working some times
问题描述
我正在使用 gulp 任务来迁移数据库.出于测试目的,我使用不同的数据库.所以我需要完全相同的数据库.我正在尝试使用 sequelize.sync({ force: true }) 但它不起作用.
i am using gulp tasks for migration of database. For testing purpose i am using different database. so i need exactly same database. i am trying to do with sequelize.sync({ force: true }) but its not working.
我的所有模型都在门户模型中.这是代码:
i am having my all models in portal-model. here is the code:
const models = require('portal-models');
gulp.task('migrate', ['create-database'], (done) => {
models.sequelize.query('SET FOREIGN_KEY_CHECKS = 0')
.then(() => models.sequelize.sync({ force: true, alter: true }))
....
....
....
)
使用 Force: true 它应该可以工作,但对我来说,我遇到了类似 mydatbaseName.tablename 不存在的错误.
With Force: true it should work but for me i am getting error like mydatbaseName.tablename is not exists.
我创建了新的测试数据库.我不想在 testdatabase 中手动创建所有内容,所以我正在使用迁移,但我 gusse 同步无法正常工作.
i have created new test database. i dont want to manually create everything in testdatabase, so i am using migrations but i gusse sync is not working properly.
谁能告诉我,我到底应该遵循什么?
Can anyone tell, exactly what should i follow?
提前致谢.
推荐答案
终于找到了错误.models.sequelize.sync({ force: true })
它工作正常,但我无法在命令提示符下看到任何日志,所以我认为同步无法正常工作.我已经添加了models.sequelize.sync({ force: true, logging: console.log }))
有了这个我能够看到日志,它正在删除所有表并再次创建.问题不在于 sequelize.sync() .问题出在我的数据库名称上,它仅指开发数据库,我正在对 testDatabase 执行查询.
Finally i found the mistake.
models.sequelize.sync({ force: true })
it was working properly but i was not able to see any logs on command prompt so i thought sync is not working properly. I have added
models.sequelize.sync({ force: true, logging: console.log }))
With this i was able to see the log, it was dropping all tables and again creating. The problem was not with sequelize.sync() . Problem was with my database name, it was referring development database only, and i was executing queries on testDatabase.
在运行测试之前,我只是更改了我的数据库,例如export MYSQL_DATABASENAME = test_database_name
.问题解决了.
before running test, i just changed my database like
export MYSQL_DATABASENAME = test_database_name
.
And issue solved.
这篇关于sequelize.sync({ force: true }) 有时无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!