Doctrine2没有将序列设置为id列(postgres)的默认值 [英] Doctrine2 doesen't set sequence to default for id column (postgres)

查看:218
本文介绍了Doctrine2没有将序列设置为id列(postgres)的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的例子:如果我想在postgres中创建一个带有auto fill id的表,我运行这个sql:

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;

但是当我生成sql(php doctrine orm:schema-tool:create --dump-sql)我得到它:

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

但不要设置为默认

\d人

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


推荐答案

精细手册


4.8.1。标识符生成策略

...

AUTO (默认):告诉学说选择策略被使用的数据库平台优选。首选策略是MySQL,SQLite和MsSQL的IDENTITY以及Oracle和PostgreSQL的SEQUENCE。这个策略提供了完整的可移植性。

...

IDENTITY :告诉原则在数据库中使用特殊的身份列生成一行插入的值。此策略目前尚未提供完整的可移植性,并受以下平台的支持:MySQL / SQLite(AUTO_INCREMENT),MSSQL(IDENTITY)和PostgreSQL(SERIAL)。

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).

他们建议 AUTO 以获得最大的可移植性:

They suggest AUTO for maximum portability:

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

应该为您创建并连接序列。另一种方法是使用 IDENTITY 策略来请求系列列:

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")
 */

这个应该创建你的 id 列为类型 serial ,PostgreSQL将为您创建序列并设置默认值。

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.

尝试使用策略= AUTO。如果不行,请尝试 strategy =IDENTITY

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

这篇关于Doctrine2没有将序列设置为id列(postgres)的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆