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
Add primary_key_default option to create table migrations #46353
base: main
Are you sure you want to change the base?
Conversation
9ce8a9a
to
259360d
Compare
This could use an update to https://github.com/rails/rails/blob/main/guides/source/active_record_postgresql.md as well. |
Thanks @bdewater I'll work on updating that documentation as well |
259360d
to
c5b9e93
Compare
@bdewater I updated the active record postgres documentation. |
efade76
to
5262fda
Compare
This allows setting the `default:` argument when creating a migration rails generate migration create_books --primary_key_type=uuid --primary_key_default="uuid_generate_v4()" class CreateBooks < ActiveRecord::Migration[7.0] def change create_table :books, id: :uuid, default: "uuid_generate_v4()" do |t| end end end It also allows you to create an initializer to configure the generator to use the `primary_key_default` value for all new create table migrations. Rails.application.config.generators do |g| g.orm(:active_record, primary_key_type: :uuid) g.orm(:active_record, primary_key_default: "\"uuid_generate_v4()\"") end
5262fda
to
82592dc
Compare
please fix the conflict 😅 |
This makes sense to me, if we have |
```ruby | ||
class CreateBooks < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :books, id: :uuid, default: "uuid_generate_v4()" do |t| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default:
syntax in create_table method is it an existing option? or just introduced in this PR?
when I checked in https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
there is no default
setting for create_table method
if it's just introduced in this PR, I would prefer to use clearer name, maybe default_uuid
what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are options
passed through for table_definition.set_primary_key
and uses this allowlist:
rails/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
Lines 1508 to 1510 in 05cb63a
def valid_primary_key_options # :nodoc: | |
[:limit, :default, :precision] | |
end |
which ultimately is just a column definition.
Motivation / Background
This Pull Request has been created because I want to automatically set the
default:
argument on create table migrations to "uuid_generate_v4()". The primary key type on my tables are :uuid and I want to use the uuid_generate_v4() method from Postgres to generate the id values.Detail
It adds the
primary_key_default
option on the migration generator, which allows an initializer to set the value to automatically use on all create table migrations.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]