# Duplicate key violates unique constraint

**URL:** <https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087>\
**Category:** Rails\
**Created:** [May 1, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087 "2007-05-01T17:37:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087/1 "2007-05-01T17:37:20Z")

</div>

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 Schedule.id += 1 but id= either  
isn’t defined or accessible in the class

Here is my code:

```
FasterCSV.foreach("schedule_store/#{@schedule_file.filename}",

```

“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]
    self.place = row[2]
    self.crn = row[3]
    self.course = row[4]
    self.title = row[5]
    self.mx = 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
    self.id += 1
    # Schedule.id += 1
  end
end

```

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

Thanks,  
Glen

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087/2 "2007-05-01T17:58:23Z")

</div>

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, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087/3 "2007-05-01T18:04:23Z")

</div>

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, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087/4 "2007-05-01T18:07:23Z")

</div>

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.

David

On May 1, 10:36 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087/5 "2007-05-01T18:27:26Z")

</div>

Sorry,

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  
controller?

On May 1, 11:03 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/duplicate-key-violates-unique-constraint/103087/6 "2007-05-01T18:46:28Z")

</div>

Thanks guys, DavidB your suggestion sounds like it might solve my  
problems.

Sean I probably should put it in a helper anyway.

Thanks again.
