# SQLite3 id not being updated immediately after save

**URL:** <https://www.ruby-forum.com/t/sqlite3-id-not-being-updated-immediately-after-save/74441>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/sqlite3-id-not-being-updated-immediately-after-save/74441 "2006-11-07T10:44:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/sqlite3-id-not-being-updated-immediately-after-save/74441/1 "2006-11-07T10:44:04Z")

</div>

Summary  
For some reason right after I save a model to the db, the id says  
‘0’…yet next transaction the db properly has it listed as the correct  
id.

Details:

I have a SQLite3 DB with the following schema:  
CREATE TABLE projects (  
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
address TEXT NOT NULL,  
created\_on INTEGER,  
updated\_on INTEGER  
);

I have a corresponding Project model.

In one of my controllers, I have the following code:  
project = Project.new  
project.address = @params[‘address’]  
if project.save  
p “Assigning id: #{project.id}!!!”  
# …stuff that relies on the id being correct…  
end

When I run that controller/view, I see “Assigning id: 0!!!” in the  
output.  
When I then do a Project.find\_all, or query the DB directly, it  
properly has an id of 1.

This occurs for all projects created by that view (not just the first).  
The id 0 problem occurs not just using project.save directly in a  
controller, but also when using the after\_create or after\_save  
callbacks inside the model.

Is this a known problem with SQLite3? Do I need to bite the bullet and  
let rails tell me what kind of DB to use?

Or is this a misunderstanding on my part? Is there something else I  
need to do to get the actual ID of the row that I’ve just created? (My  
desire is to then create a bunch of other models as defaults that are  
associated in a has\_many relationship with the Project.)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/sqlite3-id-not-being-updated-immediately-after-save/74441/2 "2006-11-07T10:44:26Z")

</div>

I’ve got something of a test case in the console here:

Slim:~/Sites/SpecBook gavinkistner$ script/console  
Loading development environment.

> > Project.find\_all  
> > =\> []
> 
> > proj = Project.new( :address=\>‘foo’, :project\_status\_id=\>1 )  
> > =\> #\<Project:0x1379438 @new\_record=true,  
> > @attributes={“created\_on”=\>nil, “project\_status\_id”=\>1,  
> > “updated\_on”=\>nil, “address”=\>“foo”}\>
> 
> > if proj.save then p proj end  
> > #\<Project:0x1379438 @new\_record\_before\_save=true,  
> > @errors=#\<ActiveRecord::Errors:0x1356244 @errors={},  
> > @base=#\<Project:0x1379438 …\>\>, @new\_record=false,  
> > @project\_status=#\<ProjectStatus:0x134bb28  
> > @attributes={“name”=\>“Internal”, “id”=\>“1”}\>,  
> > @attributes={“created\_on”=\>nil, “project\_status\_id”=\>1,  
> > “updated\_on”=\>Sun Oct 29 21:16:13 -0700 2006, “id”=\>0,  
> > “address”=\>“foo”}\>  
> > =\> nil
> 
> > proj.id  
> > =\> 0
> 
> > Project.find\_all  
> > =\> [#\<Project:0x131d868 @attributes={“created\_on”=\>nil,  
> > “project\_status\_id”=\>“1”, “updated\_on”=\>“1162181773”, “id”=\>“3”,  
> > “address”=\>“foo”}\>]

What’s going on here? Is this expected behavior?

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/sqlite3-id-not-being-updated-immediately-after-save/74441/3 "2006-11-07T10:44:26Z")

</div>

On 10/29/06, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “updated\_on”=\>nil, “address”=\>“foo”}\>
> 
> > > proj.id  
> > > =\> 0  
> > > Project.find\_all  
> > > =\> [#\<Project:0x131d868 @attributes={“created\_on”=\>nil,  
> > > “project\_status\_id”=\>“1”, “updated\_on”=\>“1162181773”, “id”=\>“3”,  
> > > “address”=\>“foo”}\>]
> 
> What’s going on here? Is this expected behavior?

I ran into this recently. The fix I did is is below.

The odd thing is, on two different, but exactly configured systems,  
one would show this problem, and the other would work fine.

I never received any responses to my emails, but ended up digging this  
solution, after several days.

Hope it helps.

* * *

For anyone else who happens to run into this. Apparently, something  
changed somewhere to cause sqlite3 and ruby to not work (some details  
here:

> **[SQLite 3 and Ruby finally working on OS X :: scottstuff.net](https://scottstuff.net/blog/articles/2006/07/01/sqlite-3-and-ruby-finally-working-on-os-x/index.html)**
>
> I’ve been trying to get Typo working with SQLite 3 on my Mac off and on for MONTHS with no success. The root problem was simple:
> a = Article.new a.title = 'Testing' a.save =\> true a.id =\> 0 Somehow, the ID wasn’t being updated whenever new...

)

I ended up installing SWIG from source (I don’t have Darwinports  
installed), and removed and reinstalled the sqlite3 gem and it’s all  
working again.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/sqlite3-id-not-being-updated-immediately-after-save/74441/4 "2006-11-07T10:44:31Z")

</div>

John T. wrote:

> I ran into this recently. The fix I did is is below.
> 
> The odd thing is, on two different, but exactly configured systems,  
> one would show this problem, and the other would work fine.
> 
> I never received any responses to my emails, but ended up digging this  
> solution, after several days.  
> [snip]

Fantastic, that did it. Thanks!  
(Also on Mac OS X, 10.4.)

I think I even read something that said “You need SWIG installed”, and  
then when the gem installed OK I said “Huh…I guess I had it  
installed.”

For the record, I did “sudo gem uninstall sqlite3-ruby”, then  
downloaded the SWIG project and built it locally, and then ran “sudo  
gem install sqlite3-ruby”.

Items properly have their ID now after saving.

Be nice if someone savvier than I could figure out the root cause of  
this (endianness?) and fix it so that no one else runs into the trouble.
