Doctrine2 doesn't set sequence to default for id column (postgres)

后端 未结 3 1653
情话喂你
情话喂你 2020-12-31 05:59

Just a simple example: If I want create a table with auto fill id in postgres I run this sql:

CREATE SEQUENCE person_id_seq  START 1;

CREATE TABLE person (
         


        
3条回答
  •  盖世英雄少女心
    2020-12-31 06:23

    I encountered this problem today and I found that:

    • IDENTITY work good, because it uses SERIAL type for PostgreSQL, which automatically creates related sequence and set default value as nextval(sequence)

    • AUTO creates table and then related sequence, but doesn't set default value for id column. It can be set by adding following code:

      /**
       * Webpage's ID
       *
       * @ORM\Id
       * @ORM\Column(type="integer", options={"default"="nextval('webpages_id_seq'::regclass)"})
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      protected $id;
      

      but unfortunately Doctrine create table first, so we need to swap SQL code creating table and sequence in order that sequence will be created first

    • SEQUENCE works the same as AUTO

提交回复
热议问题