我有gems clickhouse 0.1.10、clickhouse-activerecord 0.5.14、pg 1.4.6 和rails 7.0.4.3。 ClickHouse 服务器版本 23.4.2.11,postgresql 服务器 15.3。 我的 clickhouse 表 ddl:
create table default.tests
(
id UUID default generateUUIDv4(),
created_at DateTime64(6)
)
engine = MergeTree PARTITION BY toMonth(created_at) ORDER BY (id, created_at);
我的模型是测试。当我创建记录时:
Test.create
这会产生 sql 查询:
INSERT INTO tests (created_at) VALUES ('2023-06-10 08:41:57')
如您所见,没有精度(微秒)。 我可以像这样定义 Rails 时间数据库格式:
Time::DATE_FORMATS[:db] = '%Y-%m-%d %H:%M:%S.%6N'
然后,如果我创建记录,这将产生正确的结果:
INSERT INTO tests (created_at) VALUES ('2023-06-10 08:41:57.123123')
但这破坏了我的 PostgreSQL 模型 SQL 查询!例如,如果我有模型 PgTest 并且如果我创建这样的记录:
PgTest.create
这将在我的 postgresql 数据库中产生不正确精度的 sql 查询(重复精度):
INSERT INTO pg_tests (created_at) VALUES ('2023-06-10 08:41:57.123123.123123')
在回调 around_create 中设置时间数据库格式 - 对我来说不是一个选项。
我在 https://github.com/PNixx/clickhouse-activerecord/issues/95 上创建了问题 宝石 clickhouse-activerecord <= 0.5.15 not add precision correctly with rails 7 for insert queries. This will be fixed in next gem's versions. So i'm just waiting this.