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

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 (
    id         integer PRIMARY KEY DEFAULT nextval('person_id_seq'),
    name       varchar(100) NOT NULL
);

and in doctrine I set all property

class Person {

/**
 * @Id
 * @Column(type="integer", nullable=false)
 * @GeneratedValue(strategy="SEQUENCE")
 * @SequenceGenerator(sequenceName="person_id_seq", initialValue=1, allocationSize=100)
 */
private $id;

but when I generated sql (php doctrine orm:schema-tool:create --dump-sql) I got it:

CREATE TABLE person (
    id INT NOT NULL,
    name VARCHAR(100) NOT NULL
);
CREATE SEQUENCE person_id_seq INCREMENT BY 100 MINVALUE 1 START 1

but don't set it to default

\d person

      Column       |              Type              | Modifiers
-------------------+--------------------------------+-----------
 id                | integer                        | not null
...
..
.

Answers


From the fine manual:

4.8.1. Identifier Generation Strategies ... AUTO (default): Tells Doctrine to pick the strategy that is preferred by the used database platform. The preferred strategies are IDENTITY for MySQL, SQLite and MsSQL and SEQUENCE for Oracle and PostgreSQL. This strategy provides full portability. ... IDENTITY: Tells Doctrine to use special identity columns in the database that generate a value on insertion of a row. This strategy does currently not provide full portability and is supported by the following platforms: MySQL/SQLite (AUTO_INCREMENT), MSSQL (IDENTITY) and PostgreSQL (SERIAL).

They suggest AUTO for maximum portability:

/**
 * @Id
 * @Column(type="integer", nullable=false)
 * @GeneratedValue
 */

That should create and wire up a sequence for you. An alternative would be to ask for a serial column using the IDENTITY strategy:

/**
 * @Id
 * @Column(type="integer", nullable=false)
 * @GeneratedValue(strategy="IDENTITY")
 */

This one should create your id column as type serial and PostgreSQL will create the sequence and set up the default value for you.

The documentation indicates that what you're doing should work but the documentation usually only provides a simplified version of reality.

Try using strategy="AUTO". If that doesn't work, try strategy="IDENTITY".


Need Your Help

Unknown column 'id.state' in 'on clause'

sql where-clause

I'm getting this error can some help me?

Clojure Light Table client-server approach riddle

client-server leiningen nrepl lighttable pedestal

How can I set it up, so that I can use Light Table on the Mac, connected to a Ubuntu-hosted nREPL, and create a new program/project.clj? Can anybody help me to understand what my approach should b...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.