Using binary ids / UUIDs for database primary keys in Rails.
I write open source software, check my Github!
Ping me for collabs
Search for a command to run...
I write open source software, check my Github!
Ping me for collabs
No comments yet. Be the first to comment.
AppSignal deploy markers are a way to keep track of what git commit the bug happened, or when the slowdown started appearing in your app. https://docs.appsignal.com/application/markers/deploy-markers.html As long as you have an APP_REVISION environm...
Hey there, we're going to walk through deploying a Phoenix app to a DigitalOcean droplet, manually - no tools no nothing. Just straight up BASH! But don't worry, I've done the hard work so you don't have to. It occurred to me this week after seeing s...
It's been really hard to figure this out and I had to cobble together many sources to get it working. Enjoy! We're going to build a direct image upload to Cloudflare B2 from the browser. That way the file doesn't travel to your servers and slow them...

Uploads files from your browser directly to your bucket.

It's generally a good idea to use binary ids / UUIds for the primary keys in your database.
Here's how to easily do it in Rails 7, using Postgres!
Create a file in config/initializers/generators.rb
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
Now when you generate models it'll automatically use uuids for the primary keys. This even works automatically for things like action_text.
Here's what the action_text generator will create for you:
# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types
create_table :action_text_rich_texts, id: primary_key_type do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.timestamps
t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
end
end
private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
Notice the id type! Pretty nifty.
When you start creating more tables, make sure the type is correct.
class CreateQuotes < ActiveRecord::Migration[7.1]
def change
create_table :quotes, id: :uuid do |t|
t.text :favorite_quote
t.uuid :user_id
t.timestamps
end
end
end
t.uuid! Don't forget!
And it's that easy.