简体   繁体   中英

Using PostgreSQL DEFAULT with Doctrine?

I have a PostgreSQL database I would like to use with Doctrine. The database has DEFAULT values on some of the fields (for example like default nextval('transfer_seq'::regclass) ).

When I try to do a orm:schema-tool:update to alter the database, everything works otherwise ok, but Doctrine insists on dropping all of the pre-existing defaults, with ALTER TABLE ext_requests DROP DEFAULT .

Of course, it's somewhat trivial to manually run the changes to database just ignoreing the lines with DROP DEFAULT, but I'm looking to automating the process, so it seems silly not to be able to just use the update command directly.

Is there a way to tell Doctrine schema-tool to ignore any alterations to PostgreSQL default values?

(I know I could use Doctrine @PrePersist to set defaults , but since Doctrine isn't the only client using the database this isn't really a solution.)

I could be wrong, but i think what your asking is, to make use of the identity function on doctrines 'GeneratedValue' Strategy.

The following snippet, will make use of Postgres 'nextval()' functionality on a 'primary key' column.

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

By assigning as strategy IDENTITY, Doctrine will listen to the database it's default value.

Not sure if there is a identical solution for other 'default' values (non-primary columns)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM