这是一个非常奇怪的问题。 如果我通过Sequelize.import()定义模型,则Sequelize无法创建记录-返回错误“从字符串转换日期和/或时间时转换失败”。但是,如果我在主模块中使用Sequelize.define()定义了SAME模型,则它可以完美地工作。作为Sequelize的新手,这让我感到困惑。
这是模型文件中的代码
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Sale', {
SaleID: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
SaleTypeID: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'SaleTypeMaster',
key: 'SaleTypeID'
}
},
LocationID: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Location',
key: 'LocationID'
}
},
TillID: {
type: DataTypes.INTEGER,
allowNull: false
},
SaleStartDateTime: {
type: DataTypes.DATE,
allowNull: false
},
SystemCashierID: {
type: DataTypes.INTEGER,
allowNull: true
},
ShiftID: {
type: DataTypes.INTEGER,
allowNull: true
},
SaleTotalInc: {
type: DataTypes.DECIMAL,
allowNull: true
},
TaxTotal: {
type: DataTypes.DECIMAL,
allowNull: true
},
HostCashierName: {
type: DataTypes.STRING,
allowNull: true
},
HostSaleID: {
type: DataTypes.STRING,
allowNull: true
},
HostAlternateSaleID: {
type: DataTypes.STRING,
allowNull: true
},
NavSaleExport: {
type: DataTypes.INTEGER,
allowNull: true
}
}, {
tableName: 'Sale',
timestamps: false
});
};
这是主模块
const EMCSale = etlDb.import("./etl/Sale");
EMCSale.sync();
/*
const EMCSale = etlDb.define('Sale', {
SaleID: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
SaleTypeID: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'SaleTypeMaster',
key: 'SaleTypeID'
}
},
LocationID: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Location',
key: 'LocationID'
}
},
TillID: {
type: Sequelize.INTEGER,
allowNull: false
},
SaleStartDateTime: {
type: Sequelize.DATE,
allowNull: false
},
SystemCashierID: {
type: Sequelize.INTEGER,
allowNull: true
},
ShiftID: {
type: Sequelize.INTEGER,
allowNull: true
},
SaleTotalInc: {
type: Sequelize.DECIMAL,
allowNull: true
},
TaxTotal: {
type: Sequelize.DECIMAL,
allowNull: true
},
HostCashierName: {
type: Sequelize.STRING,
allowNull: true
},
HostSaleID: {
type: Sequelize.STRING,
allowNull: true
},
HostAlternateSaleID: {
type: Sequelize.STRING,
allowNull: true
},
NavSaleExport: {
type: Sequelize.INTEGER,
allowNull: true
}
},
{
tableName: 'SaleX',
timestamps: false
});
*/
var newSaleRec = {
SaleTypeID: 0,
LocationID: 100,
TillID: 1,
SaleStartDateTime: "2019-03-15T12:00:00.000"
}
var User = etlDb.define('user', {
username: Sequelize.STRING,
birthday: Sequelize.DATE
});
etlDb.sync().then(function() {
return User.create({
username: 'janedoe',
birthday: "2019-03-12T12:00:00.000"
});
}).then(function(jane) {
console.log(jane.get({
plain: true
}));
return EMCSale.create(newSaleRec);
}).then(function(sale) {
console.log(sale.get({
plain: true
}));
}).catch(err => {
console.log("Done with error "+err);
})
这是输出-首先是使用定义时,然后是使用导入时