【问题标题】:Postgres integer out of rangePostgres 整数超出范围
【发布时间】:2020-03-22 08:11:10
【问题描述】:

我在尝试插入全新数据库上的全新表时收到value "1574799549227" is out of range for type integer 错误。我正在使用带有 pg 模块的节点。

db
.query(`INSERT INTO users (uuid, email, password, created_at, updated_at) VALUES ($1, $2, $3, to_timestamp($4 / 1000), to_timestamp($4 / 1000))`,
            [uuid, email, password, Date.now()])
.catch((err) => {
    console.log(err);
});

这是桌子

CREATE TABLE public.users
(
    id serial,
    uuid uuid,
    email character varying,
    password character varying,
    created_at timestamp with time zone,
    updated_at timestamp with time zone,
    CONSTRAINT user_id_pk PRIMARY KEY (id)
)

我也尝试过重置 id 字段的序列号

【问题讨论】:

  • 使用 bigserial 或 bigint 而不是 serial 或 uuid
  • @nacho 我已将它切换到 bigserial,它不能解决问题。这也不应该是一个问题,因为数据库和表是全新的..
  • 1 美元有什么价值??

标签: sql node.js database postgresql


【解决方案1】:

我最终弄明白了。原来这是我拥有的时间戳。我应该使用to_timestamp($4 / 1000.0)时使用to_timestamp($4 / 1000)

【讨论】:

    【解决方案2】:

    请参阅Numeric types 的官方文档。串行数据类型的范围是 1 到 2147483647。

    SERIAL 列存储为 INTEGER,最大值为 231-1。因此,在大约 20 亿次插入之后,您的新 id 值将不再适合,因此请确保您在表的生命周期内插入了那么多行。

    如果您要更改表格,请记住使用BIGINT 而不是BIGSERIAL

    ALTER TABLE users ALTER COLUMN id TYPE BIGINT;

    最可能的原因是id 列中的值超出此范围。

    【讨论】:

    • 可能我没有表达清楚。表是新的,里面已经没有数据了。 ID 是串行的,所以它会自动递增,所以理论上它应该是 1
    • 您是否尝试过使用 col id 作为 BIGSERIAL 重新创建表或更改表以将 id col 更改为 BIGINT?
    • 是的,正如我在上面向 nacho 解释的那样。我试过切换它们
    猜你喜欢
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多