> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/columns-with-default-values-are-nil-on-create.md).

# 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)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/columns-with-default-values-are-nil-on-create.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
