gpt4 book ai didi

mysql - 防止 Sequelize 插入时间戳

转载 作者:行者123 更新时间:2023-11-29 16:34:42 24 4
gpt4 key购买 nike

有很多类似的问题,但我已经尝试了大多数建议的解决方案,但没有运气。

我已经有数据库(mysql),所以我跳过了模型定义并使用了sequelize-auto(编程)。第一个问题与已弃用的依赖项有关(sequelize-auto 使用sequelize v3)。

通过sequelize-auto从现有数据库生成模型(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
}
}

create.sql 中的列 updated_at 定义:

updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

因此生成的模型完全丢失ON UPDATE CURRENT_TIMESTAMP

生成模型后,我将它们与express一起用于sequelize v4。

幸运的是,可以在模型定义中设置timestamps: false,但它不起作用。我还尝试了 updated_at: false (与 underscored: true 一起),但没有运气(它根本没有效果 - 仍然是同样的错误)。

唯一有效的方法是在定义之前从模型中完全删除 updated_at 键。之后,一切都像魅力一样,但我需要过滤此列,所以它不是解决方案。

模型.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`);

数据库错误:

Unhandled rejection SequelizeDatabaseError: Incorrect datetime value: 'CURRENT_TIMESTAMP' for column 'updated_at' at row 1

有什么建议吗?

最佳答案

你的日期完全错误。您在 mysql 表中声明为时间戳。但您将 CURRENT_TIMESTAMP 插入为 String

Executing (default): INSERT INTO `bb_campaign` .... ,'**CURRENT_TIMESTAMP**') ON ....;

它应该是 NOW() 而不带单引号。

   "updated_at":{
"type":"TIMESTAMP",
"allowNull":false,
"defaultValue":**"CURRENT_TIMESTAMP"**,
"primaryKey":false
}

上面的一个,又是字符串。它应该是不带双引号的 NOW()

var user = sequelize.define('mytable', {  }, {
....
timestamps: false,
....
});

关于mysql - 防止 Sequelize 插入时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53688897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com