【发布时间】:2014-11-21 15:22:01
【问题描述】:
我正在尝试运行一个脚本以在通过sequelize.sync({ force: true }); 进行同步之前从数据库中删除所有表
当我从控制台运行脚本时,脚本运行没有问题,当我尝试从我的 node.js 应用程序运行它时会出现问题; MySql 返回解析错误。
node.js
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,人们似乎也遇到了同样的问题,但我不知道解决方案是什么。
【问题讨论】:
-
顺便说一下,我得到了这个工作脚本; stackoverflow.com/questions/12403662/drop-all-tables/…
标签: mysql node.js sequelize.js