我已经有了数据库(mysql),所以我跳过了模型定义,使用了sequelize auto(编程)。第一个问题是不推荐使用的依赖项(sequelize auto use sequelize v3)。
{
"id":{
"type":"INT(10) UNSIGNED",
"allowNull":false,
"defaultValue":null,
"primaryKey":true,
"foreignKey":{
"source_table":"db_name",
"source_schema":"db_name",
"target_schema":null,
"constraint_name":"PRIMARY",
"source_column":"id",
"target_table":null,
"target_column":null,
"extra":"",
"column_key":"PRI",
"isPrimaryKey":true
}
},
"name":{
"type":"VARCHAR(255)",
"allowNull":false,
"defaultValue":null,
"primaryKey":false
},
"start":{
"type":"DATE",
"allowNull":false,
"defaultValue":null,
"primaryKey":false
},
"end":{
"type":"DATE",
"allowNull":false,
"defaultValue":null,
"primaryKey":false
},
"active":{
"type":"TINYINT(1)",
"allowNull":false,
"defaultValue":"0",
"primaryKey":false
},
"updated_at":{
"type":"TIMESTAMP",
"allowNull":false,
"defaultValue":"CURRENT_TIMESTAMP",
"primaryKey":false
}
}
柱
updated_at
create.sql中的定义:
updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
.
生成模型后,我将它们与express一起用于sequelize v4。
timestamps: false
updated_at: false
(连同
underscored: true
唯一有效的方法是完全移除
在定义之前,从模型中键入。在那之后,一切都很有魅力,但我需要过滤这个专栏,所以它不是解决方案。
model.js:
const SequelizeAuto = require("sequelize-auto");
const Sequelize = require("sequelize");
const config = require("../conf/config.json");
const Op = Sequelize.Op; // fixed sequelize v3 bug
var options = {
database: config.db.name,
username: config.db.username,
password: config.db.password,
host: config.db.host,
dialect: config.db.dialect,
operatorsAliases: Op,
pool: {
max: 5,
min: 0,
idle: 10000
},
directory: false, // prevents the program from writing to disk
//port: 'port',
//tables: [],
additional: {
underscored: true,
timestamps: false
}
};
const modelOptions = {
bb_campaign: {
alias: "campaign"
},
bb_campaign_stats: {
alias: "stat"
}
};
function getTableOptions(tableName) {
return {
freezeTableName: true,
underscored: true,
timestamps: false,
updated_at: false,
tableName: tableName
}
}
// SequelizeAuto uses old sequelize v3 + mysql (not mysql2)
var db_map = new SequelizeAuto(config.db.name, config.db.username, config.db.password, options);
module.exports = new Promise((resolve, reject) => db_map.run((err) => {
if (err) {
reject(err);
return;
}
// atm session is closed, we need to create new
// https://github.com/sequelize/sequelize-auto/issues/243
let db = {};
db.sequelize = new Sequelize(options);
for (tableName in db_map.tables) {
let alias = tableName in modelOptions ?
modelOptions[tableName].alias : tableName;
db[alias] = db.sequelize.define(alias, db_map.tables[tableName], getTableOptions(tableName));
console.log('Registered model for table %s as %s.', tableName, alias);
// asociace ted nepotrebujeme
//if (tableName in db_map.foreignKeys) {}
}
resolve(db);
}));
插入:
Executing (default): INSERT INTO `bb_campaign` (`id`,`name`,`start`,`end`, `active`,`updated_at`) VALUES ('216450','item name','2018-12-02','2018-12-08',1,'CURRENT_TIMESTAMP') ON DUPLICATE KEY UPDATE `id`=VALUES(`id`), `name`=VALUES(`name`), `start`=VALUES(`start`), `end`=VALUES(`end`), `active`=VALUES(`active`);
未处理的拒绝SequelizeDatabaseError:日期时间值不正确:
有什么建议吗?