Forum: Ruby on Rails duplicate key violates unique constraint

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 (Guest)
on 2007-05-01 17:37
(Received via mailing list)
I've run into an interesting issue.  When setting up a table with data
from a file (I'm doing this in a block).  I find that I can't create
separate entries manually after the import.  It complains about a
duplicate primary key.  I've tried += 1 but id= either
isn't defined or accessible in the class

Here is my code:

"r") do |row|
      unless row[3] == "CRN"

        # This would be faster as straight SQL if the current method
        # slows down too much.
        # Would probably be cleaner with straight SQL too.

        self.associate(row[0], row[1])

        self.first_name = row[0]
        self.last_name = row[1] = row[2]
        self.crn = row[3]
        self.course = row[4]
        self.title = row[5] = row[6]
        self.enr = row[7]
        self.avl = row[8]
        self.days = row[9]
        self.start_time = row[10]
        self.end_time = row[11]
        self.start_date = row[12]
        self.end_date = row[13]

        self.create += 1
        # += 1

Anyone know how I can get Rails to realize what the current id is
after the automated import?

8716cce9a7b721786e5425b8cc1f9951?d=identicon&s=25 unknown (Guest)
on 2007-05-01 17:58
(Received via mailing list)
Is each row instance creating a new id and therefore starting at 1?
Is each row an active record object of the same kind?

On May 1, 10:36 am, "" <>
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 (Guest)
on 2007-05-01 18:04
(Received via mailing list)
If I put the code in the controller rather than the model and do a
save rather than create it works fine.  I'd rather put this code in
the model though.

Also how do you reset the id sequence back to one?  I need to do this
when I clear out the old data and import new data as these schedules
could possibly get rather long and I don't know how long this program
will be used.

On May 1, 9:36 am, "" <>
8e01a4d029c6073279ae7287665d5550?d=identicon&s=25 DavidB (Guest)
on 2007-05-01 18:07
(Received via mailing list)
I don't know exactly what the cause of your problem is, but I have a
suspicion. In postgres, you may have a table called 'schedules' and
there will also then be a table called 'schedules_id_seq'. This
specifies the last id used in schedules. If your last serial id in
schedules was 499, then this will be noted in schedules_id_seq. This
value in schedules_id_seq needs to be adjusted to the highest id of
the rows that you imported back after recreating the table, otherwise
trying to insert anything with an id of less than 499 will give
precisely the type of error your server[and thus you] raised.

I hope this is helpful.


On May 1, 10:36 am, "" <>
8716cce9a7b721786e5425b8cc1f9951?d=identicon&s=25 unknown (Guest)
on 2007-05-01 18:27
(Received via mailing list)

I thought you were running a script outside of your app to add data to
the database.

Maybe you can put the code in a helper and call it from the

On May 1, 11:03 am, "" <>
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 (Guest)
on 2007-05-01 18:46
(Received via mailing list)
Thanks guys,  DavidB your suggestion sounds like it might solve my

Sean I probably should put it in a helper anyway.

Thanks again.
This topic is locked and can not be replied to.