Duplicate pkey on heroku

40 views
Skip to first unread message

Sebastien Cesbron

unread,
Sep 19, 2011, 2:45:39 PM9/19/11
to play-fr...@googlegroups.com
Hi

I am testing heroku. I have deployed my app and inserted some sample data through insert statements via DB.execute. Now if I want to create new objects with my app I have a duplicate pkey exception

I am using basic ddl create with hibernate. How is the sequence handled in this case on postgresql ? Is there something I have to do (update a sequence table) to be able to reinit my sequence ?

Regards
Seb

Keith Swallow

unread,
Sep 19, 2011, 3:40:21 PM9/19/11
to play-fr...@googlegroups.com
Hi Seb,

The default way of creating ids in Postgres for Play is for there to be one master 'hibernate_sequence'. Each new insert managed by the ORM calls "SELECT nextval('hibernate_sequence');". If want to set the value for a sequence you can do the following:

ALTER SEQUENCE hibernate_sequence RESTART WITH 1234;

I recommend you add a sequence for each of your models. Unfortunately I'm not 100% sure how to do this with JPA, but I do know you will have to define an ID field of your own, overriding what is in Model.

Best,

Keith

Dominik Dorn

unread,
Sep 21, 2011, 5:26:01 PM9/21/11
to play-fr...@googlegroups.com
like this:

@Entity
@Table(name = "course_document", schema = "skriptum")
public class CourseDocument extends GenericModel {
....
@SequenceGenerator(
name="course_document_PKGen",
sequenceName= "course_document_id_seq", // <-- thats the name of the
sequence of the table
schema = "skriptum",
allocationSize = 1 // set this to higher values only if you're
planing on inserting a huge amount of values at once.. else you'll
have big holes in your PK generation.
)
@GeneratedValue(generator = "course_document_PKGen", strategy =
GenerationType.SEQUENCE)
@Id
@Column(name = "id")
public long id;

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

--
Dominik Dorn
http://dominikdorn.com
http://twitter.com/domdorn

Skripten, Mitschriften, Lernunterlagen, etc. findest Du auf
http://www.studyguru.eu !

Reply all
Reply to author
Forward
0 new messages