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.