开发者

Trouble using Doctrine 2 annotation @GeneratedValue(strategy="AUTO")

开发者 https://www.devze.com 2023-03-08 00:45 出处:网络
Hope someone can help me out with this. I have the following doctrine annotation: /** * @var integer *

Hope someone can help me out with this. I have the following doctrine annotation:

/**
 * @var integer 
 *
 * @Column(name="code", type="integer", length=4)
 * @Id
 * @GeneratedValue(strategy= "AUTO")
 */
private $code;

Where code is my table Primary Key. What I want is the code value to be properly set (automatically) every time a row is inserted in the table.

I'm using Doctrine 2 and PostgreSQL and the problem is this: In order to prepare the DB in advance I insert rows into the table using SQL commands directly in PgAdmin. Then I try inserting new rows but this time through my program and every time I try to insert this row I get a Primary Key conflict until I've tried as many times as rows inserted before with SQL commands.

For example if I've inserted 3 rows using SQ开发者_高级运维L commands, I'd have to try 3 times using the program and by the 4th atempt the row would be properly inserted.

Is there any way to set the initial Primary Key value to 4 (Of course this value depends on the number of rows already inserted using SQL commands before running the program) to avoid the Primary Key conflicts?

Thanks!


I think you inserted values in your table manually self assigning a the value of the code:

insert into table (code, ... ) values (1, ...)

In that case your serial value will not be incremented. So when Doctrine tries to insert a new row to the database, it will use the serial for the auto numbering. The serial status is still at value 1, which causes a Primary key conflict. Although the query failed, the serial is incremented by one, so the next time it will try to use the value 2. This explains why after three attempts, you don't get a Primary Key conflict anymore.

Hope this explanation helps.

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号