如何使用 PostgreSQL 方言在 Spanner 中创建具有 SPANNER.COMMIT_TIMESTAMP 列的表?

问题描述 投票:0回答:1

我正在运行查询:

CREATE TABLE dump_table (
  id character varying NOT NULL,
  project_id character varying NOT NULL,
  user_id character varying NOT NULL,
  label character varying NOT NULL,
  value character varying NOT NULL,
  created_on timestamp with time zone NOT NULL,
  updated_on SPANNER.COMMIT_TIMESTAMP with time zone NOT NULL,
  deleted_on timestamp with time zone,
  PRIMARY KEY(id, project_id)
);;

我的目的是不允许列为空值:

created_on
updated_on
。此外,我想使用这个 Golang 代码来提交updated_on:

mutation := spanner.InsertOrUpdate(
        "pinectl_dump",
        []string{"id", "project_id", "user_id", "label", "value", "environment", "created_on", "updated_on", "deleted_on"},
        []interface{}{apiKey, projectID, userID, userLabel, apiKey, environment, creationTimestamp, spanner.CommitTimestamp, nil},
    )

我阅读了官方文档,其中显示了创建具有此类列的表的示例,但是当我执行(尝试)查询时,它失败并出现以下错误:

Expected "timezone"i

当我从查询中删除created_on行时,我在下一行中收到错误:

Expected "ARRAY<"i, "bigint"i, "bool"i, "boolean"i, "bytea"i, "character varying"i, "character"i, "date"i, "decimal"i, "double precision"i, "double"i, "float8"i, "int"i, "int8"i, "jsonb"i, "numeric"i, "text"i, "timestamp with timezone"i, "timestamp"i, "timestamptz"i or "varchar"i.

如何使用 PostgreSQL 方言创建 Spanner 表,以便其中的一列可用于记录带有时区的 update_on 时间戳?

sql postgresql google-cloud-platform google-cloud-spanner
1个回答
0
投票

问题在于

with time zone
类型规范后面的
SPANNER.COMMIT_TIMESTAMP
后缀。根据定义,
SPANNER.COMMIT_TIMESTAMP
始终带有时区,因此不需要也不支持
with time zone
规范。因此,以下应该有效:

CREATE TABLE dump_table (
  id character varying NOT NULL,
  project_id character varying NOT NULL,
  user_id character varying NOT NULL,
  label character varying NOT NULL,
  value character varying NOT NULL,
  created_on timestamp with time zone NOT NULL,
  updated_on SPANNER.COMMIT_TIMESTAMP NOT NULL,
  deleted_on timestamp with time zone,
  PRIMARY KEY(id, project_id)
);

© www.soinside.com 2019 - 2024. All rights reserved.