From b1b9d0594d3bac66a7b9ea80bdd128af7cc33a90 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 18 Jan 2021 18:20:32 -0600 Subject: [PATCH] Add Columns With Default Values Are Nil On Create as a rails til --- README.md | 3 +- ...s-with-default-values-are-nil-on-create.md | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rails/columns-with-default-values-are-nil-on-create.md diff --git a/README.md b/README.md index 289962e..73eb423 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1015 TILs and counting..._ +_1016 TILs and counting..._ --- @@ -619,6 +619,7 @@ _1015 TILs and counting..._ - [Check If ActiveRecord Update Fails](rails/check-if-activerecord-update-fails.md) - [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md) - [Code Statistics For An Application](rails/code-statistics-for-an-application.md) +- [Columns With Default Values Are Nil On Create](rails/columns-with-default-values-are-nil-on-create.md) - [Comparing DateTimes Down To Second Precision](rails/comparing-datetimes-down-to-second-precision.md) - [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md) - [Convert A Symbol To A Constant](rails/convert-a-symbol-to-a-constant.md) diff --git a/rails/columns-with-default-values-are-nil-on-create.md b/rails/columns-with-default-values-are-nil-on-create.md new file mode 100644 index 0000000..46c17b2 --- /dev/null +++ b/rails/columns-with-default-values-are-nil-on-create.md @@ -0,0 +1,28 @@ +# Columns With Default Values Are Nil On Create + +Let's say I have a `MagicLinks` model backed by `magic_links` Postgres table. +Both the `id` and `token` columns are of type `UUID` and have default values of +`gen_random_uuid()`. That means from the Rails-side when I go to create a +`MagicLink` record, I don't have to think about specifying values for `id` or +`token` -- the DB will take care of that. + +```ruby +> magic_link = MagicLink.create(expires_at: Time.zone.now, user: User.last) + User Load (5.9ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]] + TRANSACTION (0.1ms) BEGIN + MagicLink Create (3.1ms) INSERT INTO "magic_links" ("user_id", "expires_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" +... + +> magic_link.id +=> "6c6dddbf-4427-407d-8dc8-eef8cb65d491" +> magic_link.token +=> nil +``` + +This `create` call is translated into an `insert` SQL statement that includes a +`returning` clause. For `create` it is always `returning "id"`. This means that +the `UUID` value generated in Postgres-land for `id` gets passed back into the +ActiveRecord instance. The `UUID` value generated for `token`, however, is not +because `token` isn't specified in the `returning` clause. + +[source](https://github.com/rails/rails/issues/17605)