我正在尝试运行脚本以从数据库中删除所有表,然后通过sequelize.sync({ force: true });
续订同步
当我从控制台运行脚本时,脚本运行没有问题,当我尝试从我的node.js应用程序运行它时会出现问题; MySql返回一个解析错误。
var dropAllTables = [
'SET FOREIGN_KEY_CHECKS = 0;',
'SET GROUP_CONCAT_MAX_LEN = 32768;',
'SET @tables = NULL;',
"SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables FROM information_schema.tables WHERE table_schema = (SELECT DATABASE());",
"SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);",
"SELECT IFNULL(@tables, 'SELECT 1') INTO @tables;",
'PREPARE stmt FROM @tables;',
'EXECUTE stmt;',
'DEALLOCATE PREPARE stmt;',
'SET FOREIGN_KEY_CHECKS = 1;',
"SET GLOBAL sql_mode = 'STRICT_ALL_TABLES';"
].join(' ');
sequelize.query(dropAllTables, {
raw: true
}).then(function() {
return sequelize.sync({ force: true });
}).then(function() {
console.log('Database recreated!');
callback();
}, function(err) {
throw err;
});
{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET GROUP_CONCAT_MAX_LEN = 32768; SET @tables = NULL; SELECT GROUP_CONCAT('`', t' at line 1]
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0,
sql: 'SET FOREIGN_KEY_CHECKS = 0; SET GROUP_CONCAT_MAX_LEN = 32768; SET @tables = NULL; SELECT GROUP_CONCAT(\'`\', table_name, \'`\') INTO @tables FROM information_schema.tables WHERE table_schema = (SELECT DATABASE()); SET @tables = CONCAT(\'DROP TABLE IF EXISTS \', @tables); SELECT IFNULL(@tables, \'SELECT 1\') INTO @tables; PREPARE stmt FROM @tables; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET FOREIGN_KEY_CHECKS = 1; SET GLOBAL sql_mode = \'STRICT_ALL_TABLES\';' }
我在Google和sequelize docs page中找不到关于sequelize的多个原始查询的内容(我查找了query
方法的特定参数)。
我从一个SO克隆中找到了this thread,人们似乎遇到了同样的问题,但我无法弄清楚解决方案是什么。
答案 0 :(得分:25)
您可以使用
传递multipleStatements
选项
new Sequelize(user, pass, db, {
dialectOptions: {
multipleStatements: true
}
});
您放入dialectOptions
的任何内容都将传递给基础连接库(在本例中为mysql
)
答案 1 :(得分:4)
根据所使用的基础mysql模块,至少mysql
/ mysql2
支持multipleStatements: true
connection setting。这将允许您一次发送多个查询。默认情况下,出于安全原因,它会被禁用。
答案 2 :(得分:0)
使用以下方法修复:
dialectOptions: {
multipleStatements: true
}