Skip to content

Commit

Permalink
Support custom primary key in accounts table on MySQL
Browse files Browse the repository at this point in the history
If one wants to use UUID primary keys on MySQL, one possibility is using
a string column and generating UUIDs in Ruby.

  # table definition
  create_table :accounts do
    String :id, primary_key: true
    # ...
  end

  # rodauth configuration
  before_create_account do
    account[:id] = SecureRandom.uuid
  end

However, Sequel's `#insert` appears to return `0` when the primary key
is something other than an autoincrementing integer on MySQL. This
causes Rodauth's `#save_account` to overwrite the generated UUID with
`0` on the account hash, causing problems down the line in things like
account verification.

We fix this by having `#save_account` only set the `:id` if it hasn't
already been set.
  • Loading branch information
janko committed Nov 2, 2023
1 parent ff079e2 commit 8e437f0
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rodauth/features/create_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def save_account
end

if id
account[account_id_column] = id
account[account_id_column] ||= id
end

id && !raised
Expand Down

0 comments on commit 8e437f0

Please sign in to comment.