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

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

问题描述

一个简单的例子:如果我想在postgres中创建一个具有自动填充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

但不要将其设置为默认值

but don't set it to default

\个人

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


推荐答案

来自精细手册


4.8.1。标识符生成策略

...

AUTO (默认值):告诉Doctrine选择要采用的策略使用的数据库平台首选。首选策略是MySQL,SQLite和MsSQL的IDENTITY以及Oracle和PostgreSQL的SEQUENCE。此策略提供了完全的可移植性。

...

IDENTITY :指示Doctrine使用数据库中的特殊标识列来生成插入行时的值。该策略目前尚不能提供完全的可移植性,并且受到以下平台的支持: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.

尝试使用 strategy = AUTO 。如果那不起作用,请尝试 strategy = IDENTITY

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

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

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