mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Columns With Default Values Are Nil On Create as a rails til
This commit is contained in:
@@ -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).
|
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 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)
|
- [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)
|
- [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)
|
- [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)
|
- [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)
|
- [Convert A Symbol To A Constant](rails/convert-a-symbol-to-a-constant.md)
|
||||||
|
|||||||
28
rails/columns-with-default-values-are-nil-on-create.md
Normal file
28
rails/columns-with-default-values-are-nil-on-create.md
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user