gpt4 book ai didi

javascript - 为什么 Sequelize 时间戳没有关闭

转载 作者:行者123 更新时间:2023-12-02 21:26:55 24 4
gpt4 key购买 nike

我似乎无法理解为什么我的 Sequelize 时间戳没有关闭,尽管遵循了文档说明。我尝试在模型定义和 Sequelize 定义中设置它,使其在数据库范围内。

我尝试运行 findAll 查询,但不断收到此错误:

Executing (default): SELECT 1+1 AS result
Database connection has been established successfully.
Executing (default): SELECT "contact_id", "first_name",
"last_name","contact_type_id", "org_name", "created_at",
"updated_at", "createdAt","updatedAt" FROM "contacts" AS
"contacts";

Unhandled rejection SequelizeDatabaseError:
column "createdAt" does not exist

当然它不存在,我正在尝试将其关闭。

无论如何,这是我的模型定义和 Sequelize 定义:

contact.js

const Sequelize = require('sequelize');
var Model = Sequelize.Model;

module.exports = (sequelize, DataTypes) => {
const Contact = sequelize.define('contacts', {
contact_id: {
type: Sequelize.INTEGER,
field: 'contact_id',
primaryKey: 'true'
},
first_name: {
type: Sequelize.STRING,
field: 'first_name'
},
[...] //condensed for legibility
created_at: {
type: Sequelize.DATE,
field: 'created_at'
},
updated_at: {
type: Sequelize.DATE,
field: 'updated_at'
}
},
{
// Options
tableName: 'contacts',
timestamps: 'false'
}
);

return Contact;

};

模型\index.js

var fs        = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var db = {};

const sequelize = new Sequelize(process.env.DB, process.env.DB_USER,
process.env.DB_PASS, {
host: xxxxxxxx,
dialect: 'postgres',
protocol: 'postgres',
freezeTableName: 'true'
});

[...] // condensed for legibility

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

任何人都可以提供任何见解吗?我已经重新启动了服务器几次 - 包括后端和前端。

最佳答案

timestamps 值应该是 bool 类型,而不是字符串类型。看这个docs

  /**
* Adds createdAt and updatedAt timestamps to the model. Default true.
*/
timestamps?: boolean;

例如

/* eslint-disable @typescript-eslint/camelcase */
import { sequelize } from '../../db';
import Sequelize, { Model } from 'sequelize';

class Contact extends Model {}
Contact.init(
{
contact_id: {
type: Sequelize.INTEGER,
field: 'contact_id',
primaryKey: true,
},
first_name: {
type: Sequelize.STRING,
field: 'first_name',
},
created_at: {
type: Sequelize.DATE,
field: 'created_at',
},
updated_at: {
type: Sequelize.DATE,
field: 'updated_at',
},
},
{ sequelize, modelName: 'contacts', timestamps: false },
);

(async function test() {
try {
await sequelize.sync({ force: true });
await Contact.findAll();
} catch (error) {
console.log(error);
} finally {
await sequelize.close();
}
})();

执行结果:

Executing (default): DROP TABLE IF EXISTS "contacts" CASCADE;
Executing (default): DROP TABLE IF EXISTS "contacts" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "contacts" ("contact_id" INTEGER , "first_name" VARCHAR(255), "created_at" TIMESTAMP WITH TIME ZONE, "updated_at" TIMESTAMP WITH TIME ZONE, PRIMARY KEY ("contact_id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'contacts' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): SELECT "contact_id", "first_name", "created_at", "updated_at" FROM "contacts" AS "contacts";

如您所见,不再有 createdAtupdatedAt 列。

关于javascript - 为什么 Sequelize 时间戳没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60718864/

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