SQLite 3 and Ruby finally working on OS X
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 ActiveRecord objects were created. They’d end up in the DB just fine, but the new ID wasn’t visible from Ruby. Since this is a really basic feature of any ORM, I was really amazed that it didn’t work. A bit of research shows that SQLite and Rails work fine for most users, but I’m not alone with this problem–there are a few reports of similar problems on the Rails wiki.
For the fun of it, I installed SQLite on one of my Linux boxes, and everything worked fine there. So I tried installing a newer SQLite via DarwinPorts, with no improvement. I spent a couple hours uninstalling and reinstalling things, and nothing changed anything. Digging deeper, the bug was clearly deep inside of sqlite3-ruby–it was telling Rails that the ID number for the latest insert was 0.
I finally figured out the problem here at Gnomedex today while watching to the Gillmor Gang record their latest podcast. It’s simple:
The SQLite bindings for Ruby require SWIG. If SWIG isn’t installed, then they’ll still work, sort of, but they fail to fetch the ID from SQLite.
So, to fix the problem, I just ran port install swig, then gem uninstall sqlite3-ruby and gem install sqlite3-ruby.
There’s probably a endian problem in the non-SWIG SQLite Ruby code, but it’s easier (and probably faster) to just install SWIG.
This has caused me trouble in the past. Whenever I can’t remember why sqlite3 and ActiveRecord are busted on Mac OS X I visit this page:
http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3
There is also information on this page:
http://wiki.rubyonrails.org/rails/pages/HowtoUseSQLite
Unfortunately on the rails wiki the instructions to use SWIG are buried halfway down the page and printed in red.
Sqlite3 is very useful for tests and speeds them up significantly for me.
There are also some symptoms other than the one you describe, e.g. WEBrick crashing randomly, complaining about deadlocks.
I first experienced this instability over a year ago, and since at the time there wasn’t much information available regarding the cause, took it to be a general Rails problem. Due to time constraints I had to give up on using Rails for that particular project.
This problem is common enough that it might certainly be sensible for the SQLite3 gem to print out some warnings when being installed on a system sans SWIG, giving the user a chance to either approve installation of the pure-Ruby bindings or just abort.