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.
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.
Last updated