# Overwrite primary key when importing with ActiveRecord

**URL:** <https://www.ruby-forum.com/t/overwrite-primary-key-when-importing-with-activerecord/130220>\
**Category:** Rails\
**Created:** [February 26, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/overwrite-primary-key-when-importing-with-activerecord/130220 "2008-02-26T14:31:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [February 26, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/overwrite-primary-key-when-importing-with-activerecord/130220/1 "2008-02-26T14:31:17Z")

</div>

In my migrations I am importing data from a CSV file.

So i first create the table :

create\_table :customers do |t|  
…  
end

and then import data from a CSV into it. The CSV data allready contains  
values for the id field. My problem is that activerecord won’t let me  
override the id field :

Customer.create(:id=\>1000, :name=\>…)

It uses its own ID.

Does anyone know how I can solve this the correct way?

Any help is appreciated.

Thanks  
Chris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [February 26, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/overwrite-primary-key-when-importing-with-activerecord/130220/2 "2008-02-26T14:43:11Z")

</div>

On Feb 26, 2008, at 8:31 AM, Chris R. wrote:

> values for the id field. My problem is that activerecord won’t let me  
> Thanks  
> Chris

customer = Customer.new(:name =\> …) do |c|  
c.id = 1000  
end  
customer.save

You can’t set the id from the hash, but you can set it separately.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [February 26, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/overwrite-primary-key-when-importing-with-activerecord/130220/3 "2008-02-26T14:47:42Z")

</div>

Thanks Rob, you’ve saved me writing some ugly code to get around it.

What’s the rational behind that behavior?

Chris

> On Feb 26, 2008, at 8:31 AM, Chris R. wrote:
> 
> > values for the id field. My problem is that activerecord won’t let me  
> > Thanks  
> > Chris
> 
> customer = Customer.new(:name =\> …) do |c|  
> c.id = 1000  
> end  
> customer.save
> 
> You can’t set the id from the hash, but you can set it separately.
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [February 26, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/overwrite-primary-key-when-importing-with-activerecord/130220/4 "2008-02-26T15:38:19Z")

</div>

Well, you normally expect the :id to be auto-assigned by the database  
so it is prohibited (ignored) in the hash. Consider something like:

fred = Customer.find\_by\_name(‘fred’)  
barney = Customer.new(fred.attributes)  
barney.save

As long as there are no other uniqueness constraints, barney should be  
saved and have his own id.

You can use attr\_protected to pass this behavior to other columns of  
your choosing.

-Rob
