如何在 Sequelize 中类型为 TIME 的列中创建位置?
Posted
技术标签:
【中文标题】如何在 Sequelize 中类型为 TIME 的列中创建位置?【英文标题】:How to make a where in a column with type TIME in Sequelize? 【发布时间】:2020-03-03 13:01:50 【问题描述】:我有一些我使用 DataType.Time 的表:
startTime:
type: DataTypes.TIME,
allowNull: false
当我提出请求时,结果是这样的:
"startTime": "14:27:00"
但是,如果我尝试在此列“startTime”中使用“14:27:00”创建一个简单的 WHERE,则不起作用。结果是无效的,有人知道为什么吗?
谢谢大家!
【问题讨论】:
我会检查您的数据库以查看startTime
是否不止 14:27:00。 Sequelize 为 postgres 使用带区域的时间戳 DataTypes.TIME
man.... 不开玩笑,我什么也没做,现在可以工作 x(
【参考方案1】:
这是一个使用 "sequelize": "^5.21.3"
的工作示例:
index.ts
:
import sequelize from '../../db';
import Model, DataTypes from 'sequelize';
class SomeModel extends Model
SomeModel.init(
startTime:
type: DataTypes.TIME,
allowNull: false,
,
,
sequelize, modelName: 'some_models' ,
);
(async function test()
try
await sequelize.sync( force: true );
// seed
await SomeModel.create( startTime: '14:27:00' );
// test
const rval = await SomeModel.findOne( where: startTime: '14:27:00' , raw: true );
console.log(rval);
catch (error)
console.log(error);
finally
await sequelize.close();
)();
执行结果:
Executing (default): DROP TABLE IF EXISTS "some_models" CASCADE;
Executing (default): DROP TABLE IF EXISTS "some_models" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "some_models" ("id" SERIAL , "startTime" TIME NOT NULL, PRIMARY KEY ("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 = 'some_models' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): INSERT INTO "some_models" ("id","startTime") VALUES (DEFAULT,$1) RETURNING *;
Executing (default): SELECT "id", "startTime" FROM "some_models" AS "some_models" WHERE "some_models"."startTime" = '14:27:00' LIMIT 1;
id: 1, startTime: '14:27:00'
查看数据库中的数据记录:
node-sequelize-examples=# select * from "some_models";
id | startTime
----+-----------
1 | 14:27:00
(1 row)
node-sequelize-examples=# \d+ some_models;
Table "public.some_models"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+------------------------+----------------------------------------------------------+---------+--------------+-------------
id | integer | not null default nextval('some_models_id_seq'::regclass) | plain | |
startTime | time without time zone | not null | plain | |
Indexes:
"some_models_pkey" PRIMARY KEY, btree (id)
【讨论】:
以上是关于如何在 Sequelize 中类型为 TIME 的列中创建位置?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 sequelize 基于 sqlite3 中的“日期”列进行查询?
如何在 mysql Sequelize 实例中拥有数组的数据类型?
如何在 Sequelize 中使用 MySQL JSON 数据类型
在 Node 14 中使用 sequelize-cli,类型为:module
sequelize(和 sequelize-cli)queryInterface.createTable 在 PostgreSQL 上,PK id 类型为 UUID 和 defaultValue:Se