I am building a new NodeJS application with MySQL. I need to use the existing database schema. I have a mysql dump file that is loaded into the database (in a docker container). I am trying to generate models and migrations automatically and then run the migrations successfully. I am able to generate the models and migrations, however there is a SQL syntax error when running the generated migrations.
Here are the relevant versions:
Node10-alpine
"mysql": "^2.17.1",
"mysql2": "^1.6.5",
"sequelize": "^5.8.5",
"sequelize-auto": "^0.4.29",
"sequelize-auto-migrations": "^1.0.3"
I used the sequelize-auto module to generate the Models automatically. That works.
sequelize-auto -o "./models" -d sequelize_auto_test -h localhost -u username -p 5432 -x password -e mysql
I then attempted to use the sequelize-auto-migrations module to generate the Migrations and then run them automatically.
Generating the initial migration file works.
node ./node_modules/sequelize-auto-migrations/bin/makemigration --name <initial_migration_name>
However, when running the actual migration, there is a syntax error.
node ./node_modules/sequelize-auto-migrations/bin/runmigration
That works for many of the tables but then it runs into a syntax error.
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage:
'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 \') ENGINE=InnoDB\' at line 1',
sql: 'CREATE TABLE IF NOT EXISTS `osw` () ENGINE=InnoDB;' },
sql: 'CREATE TABLE IF NOT EXISTS `osw` () ENGINE=InnoDB;' }
Here is the relevant model osw.js (generated by the sequelize-auto module):
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
return sequelize.define('osw', {
OSWID: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true
},
IdentificationID: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: true,
references: {
model: 'itemidentification',
key: 'IdentificationID'
}
},
ProposedHours: {
type: DataTypes.DECIMAL,
allowNull: true
},
WorkStartDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
WorkEndDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
FormatID: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: true,
references: {
model: 'formats',
key: 'FormatID'
}
},
WorkLocationID: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: true
}
}, {
tableName: 'osw'
});
};
Here is the relevant part of the mysql dump file:
CREATE TABLE `OSW` (
`OSWID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`IdentificationID` int(10) unsigned DEFAULT NULL,
`ProposedHours` decimal(10,2) DEFAULT NULL,
`WorkStartDate` date DEFAULT NULL,
`WorkEndDate` date DEFAULT NULL,
`FormatID` int(10) unsigned DEFAULT NULL,
`WorkLocationID` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`OSWID`),
KEY `OSW_FKIndex1` (`IdentificationID`),
KEY `OSW_Format` (`FormatID`),
CONSTRAINT `OSW_Format` FOREIGN KEY (`FormatID`) REFERENCES `formats` (`formatid`) ON DELETE SET NULL,
CONSTRAINT `OSW_Ident` FOREIGN KEY (`IdentificationID`) REFERENCES `itemidentification` (`identificationid`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1147 DEFAULT CHARSET=utf8 PACK_KEYS=0;
UPDATE: I think the issue might be related to the migration that was generated automatically. The migration file seems to be missing the column and field type definitions, so that might be why the SQL CREATE table command is missing the column names. Here is the relevant part of the migration file that was generated regarding the osw table:
var migrationCommands = [{
{
fn: "createTable",
params: [
"osw",
{
},
{}
]
}
];



所有评论(0)