Getting started Guide: No id after save

Hi all,

I am new to RoR and try to follow the startup guide to get up to speed. Somehow I have the following issue and could not find anything useful yet.

I have created a new project and a model ‘Item’. Within the rails console, I created an item. According to the startup guide, the ID is not set in the object.

Thanks for any hints.

Setup: Linux on ChromeOS Database: SQLite rails --version Rails 7.1.3.2 ruby --version ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]

Here is the log from Rails console:

irb(main):002> item = Item.new(name:“Test”) irb(main):003> item.save TRANSACTION (0.1ms) begin transaction Item Create (0.8ms) INSERT INTO “items” (“name”, “created_at”, “updated_at”) VALUES (?, ?, ?) [[“name”, “Test”], [“created_at”, “2024-04-26 17:38:06.038873”], [“updated_at”, “2024-04-26 17:38:06.038873”]] TRANSACTION (0.2ms) commit transaction => true irb(main):004> item => #<Item:0x0000557ef0af1440 id: nil, name: “Test”, created_at: Fri, 26 Apr 2024 17:38:06.038873000 UTC +00:00, updated_at: Fri, 26 Apr 2024 17:38:06.038873000 UTC +00:00>

Hi :slight_smile:

Can you show the table description from SQLite? The id should be generated by the database - maybe the id column has not been setup correctly.

Do like this:

  • get out of the Rails console
  • get into the db console instead, with the “rails db” command at the command line, in your project’s folder
  • once inside the sqlite cli, type “.schema items”
  • copy the output of that command in a reply here

The output should contain something like “id” integer PRIMARY KEY AUTOINCREMENT NOT NULL.

Hi,

thanks for looking into it.

sqlite> .schema items CREATE TABLE IF NOT EXISTS “items” (“id” integer PRIMARY KEY AUTOINCREMENT NOT NULL, “name” varchar, “created_at” datetime(6) NOT NULL, “updated_at” datetime(6) NOT NULL);

Looks ok.

In the meanwhile I tried to have the same setup with a postgres db, and there it works as described. Saving the item populates back the id.

1 Like

Postgres has kinda become the “gold standard” for database things with Rails. (SQLite is cool, don’t get me wrong, but is best used when you only have tons of reads and not much writing going on.)

The application I am building is exactly this. It does not need much writing and I want to keep it as simple as possible. Furthermore, Rails uses SQLite as default if nothing is specified. If the getting started guide is not working as expected, it’s a pity.

I created a new app to test with SQLite, and it works as expected. If everything works correctly with Postgres, could there be something with your specific installation of SQLite?

Interesting. Would you mind to tell me the version of your SQLite3 installation?

Mine is (rather old):

sqlite3 --version 3.34.1 2021-01-20 14:10:07 10e20c0b43500cfb9bbc0eaa061c57514f715d87238f4d835880cd846b9ealt1

I have changed the following

'# Use sqlite3 as the database for Active Record

gem “sqlite3”, “~> 1.7.3” # from “~> 1.4”

and upgraded to Debian 12 (I am on a Chromebook). This comes now with SQLite3 version 3.40.1.

Now its working as expected.

1 Like

Glad to hear (well, read…) that it works now! Good luck with your application! :slight_smile:

1 Like