rails icon indicating copy to clipboard operation
rails copied to clipboard

Create returns "id: 0" when id type is set to UUID when using MySQL

Open Shuttleu opened this issue 1 year ago • 3 comments

Steps to reproduce

When creating a new rails app with nothing but using the MySQL adaptor, using a UUID as the primary key causes ActiveRecord to return id: 0 when creating a new record.

rails new app --database=mysql

Migration

class CreateUsers < ActiveRecord::Migration[7.0]
    def change
        create_table :users, id: :uuid, default: -> { "UUID()" } do |t|
            t.string :name
      
            t.timestamps
        end
    end
end

Expected behavior

When creating a new record with the id being a UUID type, the returned object should have the correct id.

Actual behavior

When creating a new record, and assigning it to a variable, the variable returned has id: 0

3.0.2 :021 > foo = User.create(name: "Foo")

  TRANSACTION (0.3ms)  BEGIN
  User Create (0.4ms)  INSERT INTO `users` (`name`,`created_at`, `updated_at`) VALUES ('Foo', '2022-08-09 10:28:08.926529', '2022-08-09 10:28:08.926529')
  TRANSACTION (2.3ms)  COMMIT
 => #<User:0x000056216d6f1a28 id: 0, name: "Foo", created_at: Tue, 09 Aug 2022 10:28:08.926529000 UTC +00:00, updated_at: Tue, 09 Aug 2022 10:28:08.926529000 UTC +00:00>

3.0.2 :021 > foo

 => #<User:0x000056216d6f1a28 id: 0, name: "Foo", created_at: Tue, 09 Aug 2022 10:28:08.926529000 UTC +00:00, updated_at: Tue, 09 Aug 2022 10:28:08.926529000 UTC +00:00>

3.0.2 :021 > foo.id

 => 0

3.0.2 :001 > User.first

  User Load (0.5ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
 =>
#<User:0x000055698f9e02e8
 id: "4377c7f9-17ce-11ed-b8d5-7cd30a13200b",
 name: "Foo",
 created_at: Tue, 09 Aug 2022 10:28:08.926529000 UTC +00:00,
 updated_at: Tue, 09 Aug 2022 10:28:08.926529000 UTC +00:00>

3.0.2 :021 > User.first.id

  User Load (0.8ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
 => "4377c7f9-17ce-11ed-b8d5-7cd30a13200b"

3.0.2 :002 > foo = User.first

  User Load (0.6ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
 =>
#<User:0x000055698fc801d8
...

3.0.2 :021 > foo.id

 => "4377c7f9-17ce-11ed-b8d5-7cd30a13200b"

Note

I'm assuming this is because the id is generated using a function on the server side? But this seems to work fine when using an integer with auto increment (which is also done server side)

System configuration

Rails version: Rails 7.0.3.1

Ruby version: Ruby 3.0.2

MySQL server version: MariaDB 10.7.4

Shuttleu avatar Aug 09 '22 10:08 Shuttleu

This and #45793 are related.

fatkodima avatar Aug 09 '22 11:08 fatkodima

Had a thought they might be,

Not entirely to sure where to start looking, otherwise I'd look into it myself.

Shuttleu avatar Aug 09 '22 11:08 Shuttleu

Relevant https://github.com/rails/rails/pull/45086.

fatkodima avatar Aug 09 '22 14:08 fatkodima

We don't take feature requests on the issue tracker, we reserve it for issues. Would you start a discussion at https://discuss.rubyonrails.org/ or create a pull request? Thanks.

Refer to https://github.com/rails/rails/issues/45847#issuecomment-1240340180

yahonda avatar Sep 11 '22 01:09 yahonda