【问题标题】:column "birthdate" is of type timestamp without time zone but expression is of type text“生日”列的类型是没有时区的时间戳,但表达式的类型是文本
【发布时间】:2021-12-27 00:05:25
【问题描述】:

此查询引发错误:列“birthdate”是没有时区的时间戳类型,但表达式是文本类型

INSERT INTO profile.profile(name,gender,birthDate,userId)
      SELECT 
          userId,
          substr(md5(random()::text), 0, 5) AS name,
          substr(md5(random()::text), 0, 2) AS gender,
          to_timestamp('2021-08-09 13:57:40', 'YYYY-MM-DD hh24:mi:ss') AS birthDate
      FROM 
          generate_series(1,10) AS y(userId)

我的桌子:

      CREATE TABLE profile.profile 
        (
          id SERIAL NOT NULL, 
          name character varying NOT NULL, 
          gender character varying NOT NULL, 
          birthDate TIMESTAMP NOT NULL, 
          image character varying NOT NULL DEFAULT 
            'https://e7.pngegg.com/pngimages/274/947/png-clipart-computer-icons-user-business-believer-business-service-people.png',
          userId integer NOT NULL, 
          CONSTRAINT UQ_profile_user UNIQUE (userId), 
          CONSTRAINT PK_profile PRIMARY KEY (id)
        )

我做错了什么?提前致谢。

【问题讨论】:

  • 您的插入语句正在尝试将性别插入到birth_date 列中。所有人的订单/数据都是错误的。

标签: postgresql insert-select


【解决方案1】:

只需更改INSERT 中列的顺序,使其与要插入的值的顺序相对应:

INSERT INTO profile.profile(userId,name,gender,birthDate)

【讨论】:

    猜你喜欢
    • 2020-02-17
    • 2015-11-09
    • 2015-11-26
    • 2019-11-08
    • 2023-03-14
    • 2021-06-18
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多