Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom primary key in accounts table on MySQL #372

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

janko
Copy link
Contributor

@janko janko commented Nov 2, 2023

Following on janko/rodauth-rails#239, if one wants to use UUID primary keys on MySQL, one possibility is using a string column and generating UUIDs in Ruby.

create_table :accounts do
  String :id, primary_key: true
  # ...
end
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.

My idea was to just modify #save_account to set the :id if it wasn't already set. My only blocker is how to write tests for this. On Postgres, the primary key value is always returned because of the RETURNING clause. I was thinking of piggy backing on the $RODAUTH_SPEC_UUID path, but that's currently Postgres-only.

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.
@jeremyevans
Copy link
Owner

Thanks for the patch. This makes sense, and Sequel::Model has same behavior of not overriding an existing primary key value.

@jeremyevans jeremyevans merged commit 8e437f0 into jeremyevans:master Nov 2, 2023
15 checks passed
@janko janko deleted the custom-primary-key-mysql branch November 2, 2023 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants