【问题标题】:autoincrement id null when inserting into postgresql table插入postgresql表时自动增量id为空
【发布时间】:2014-08-10 03:08:19
【问题描述】:

大家好,第一次在这里发帖,希望我做得对,我也是 symfony ad postgresql 的新手,所以也许我犯了一个愚蠢的错误 :)

问题描述

我有一个包含地址信息的实体地址。我使用教义生成架构:架构:创建(数据库类型 Postgresql)

表已创建并且可以在数据库中看到。我需要将信息导入到此表中,该表是从开放的街道地图数据库中获取的。所以我正在尝试插入我的实体表。我遇到的问题是我没有传递一个 ID,因为它是自动递增的,但是数据库拒绝我插入到请求中,因为 id 是一个空值。

我是 symfony 和 postgresql 的新手,所以也许我犯了一个愚蠢的基本错误。我希望有一个人可以帮助我 (顺便说一句,这个项目最初是在 mysql 中,它工作得很好,因为 ID 是自动递增的)

详情如下:


实体 YAML

DD\Bundle\DDGeneratorBundle\Entity\Address:

    type:  entity
    table: address
    readOnly: true
    repositoryClass: DD\Bundle\DDGeneratorBundle\Repository\AddressRepository

    id:
        id:
            type: integer
            generator:
                strategy: AUTO

    fields:
        number:
            type: string
            length: 255
        street:
            type: string
            length: 255
        city:
            type: string
            length: 255
        postcode:
            type: string
            length: 255
        latitude:
            type: decimal
            scale: 7
        longitude:
            type: decimal
            scale: 7

    indexes:
        coordinates:
            columns: [latitude, longitude]

    lifecycleCallbacks: {  }

由 symfony 生成的表

-- Table: address

-- DROP TABLE address;

CREATE TABLE address
(
  id integer NOT NULL,
  "number" character varying(255) NOT NULL,
  street character varying(255) NOT NULL,
  city character varying(255) NOT NULL,
  postcode character varying(255) NOT NULL,
  latitude numeric(10,7) NOT NULL,
  longitude numeric(10,7) NOT NULL,
  CONSTRAINT address_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE address
  OWNER TO postgres;

-- Index: coordinates

-- DROP INDEX coordinates;

CREATE INDEX coordinates
  ON address
  USING btree
  (latitude, longitude);

插入 SQL

insert into address ("number",street,city,postcode,latitude,longitude)
select *
from dblink('dbname=osm',
            '
SELECT tag_housenumber.v as number,
tag_street.v as street,
tag_city.v as city,
tag_postcode.v as postcode,
ST_Y(ST_Transform(nodes.geom, 4326)) AS latitude,
ST_X(ST_Transform(nodes.geom, 4326)) AS longitude


FROM ways

LEFT JOIN way_tags AS tag_housenumber ON tag_housenumber.way_id = ways.id
LEFT JOIN way_tags AS tag_street ON tag_street.way_id = ways.id
LEFT JOIN way_tags AS tag_city ON tag_city.way_id = ways.id
LEFT JOIN way_tags AS tag_postcode ON tag_postcode.way_id = ways.id
INNER JOIN way_nodes ON ways.id = way_nodes.way_id
INNER JOIN nodes on way_nodes.node_id = nodes.id

WHERE tag_housenumber.k = ''addr:housenumber''
AND tag_street.k = ''addr:street''
AND tag_city.k = ''addr:city''
AND tag_postcode.k = ''addr:postcode'' ')
       as t1("number" text,street text,city text,postcode text,latitude numeric(10,7),longitude numeric(10,7));

错误

以下是我从 pgadmin3 得到的错误,其中地址详细信息已删除:

错误:“id”列中的空值违反非空约束 详细信息:失败行包含(null,X,XXXXXX,XXXX,XXXX,XXXX,XXXX)。 ****** 错误 ******

错误:“id”列中的空值违反非空约束 SQL 状态:23502 详细信息:失败行包含(null、X、XXXXXX、XXXXXX、XXXX、XXXX、XXXX)。

【问题讨论】:

  • 听起来您正在插入 null 并希望将其替换为自动增量。那是行不通的。您需要从插入中省略它,或者使用关键字DEFAULT。
  • 您好,克雷格,感谢您的回复。据我所知,我不会添加 null ,除非 postgresql 中有我不知道的特殊内容。在我上面的帖子中,标记为“插入 SQL”的部分是我正在使用的实际请求,我没有在请求中包含 id 字段。

标签: php postgresql symfony auto-increment


【解决方案1】:

解决方案

问题在于 Symfony 没有为 id 添加自动递增默认值。 Symfony 似乎不需要这个,因为它会单独请求获取新 id 所需的序列值。

我已经修改了我的 yaml 文件,以便 symfony 为 id 字段添加默认值。

显然,对于其他正在研究如何解决此问题的人来说,您需要更改默认的字符串值以匹配在 symfony 中为您的实体生成的序列名称。

DD\Bundle\DDGeneratorBundle\Entity\Address:

    type:  entity
    table: dd_address
    readOnly: true
    repositoryClass: DD\Bundle\DDGeneratorBundle\Repository\AddressRepository

    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
            options:
                default: nextval('dd_address_id_seq')

    fields:
        number:
            type: string
            length: 255
        street:
            type: string
            length: 255
        city:
            type: string
            length: 255
        postcode:
            type: string
            length: 255
        latitude:
            type: decimal
            scale: 7
        longitude:
            type: decimal
            scale: 7

    indexes:
        coordinates:
            columns: [latitude, longitude]

    lifecycleCallbacks: {  } 

希望这可以帮助处于相同情况的其他人

亲切的问候 大卫

【讨论】:

    【解决方案2】:

    试试这个:

    将实体 yaml 部分更改为:

    id:
            id:
                type: integer
                id: true
                generator:
                    strategy: AUTO
    

    【讨论】:

    • 您好 mohsenkw,谢谢您的回复。我刚刚试过这个,清除缓存,删除数据库并重新创建它。我可以看到 symfony 正在添加一个序列,我认为它是 postgresql 世界中的自动增量: CREATE SEQUENCE address_id_seq INCREMENT BY 1 MINVALUE 1 START 1;但是在运行我的脚本时,我仍然会遇到相同的错误,id 为空值。从我的问题的“错误”部分可以看出
    猜你喜欢
    • 2020-09-05
    • 2019-02-23
    • 2017-06-22
    • 2018-03-18
    • 2014-08-03
    • 2016-12-18
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多