> 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/different-ways-to-add-a-foreign-key-reference.md).

# Different Ways To Add A Foreign Key Reference

A foreign key reference creates a relationship between two tables that is guaranteed by a foreign key constraint.

This is a minimal example.

```ruby
create_table :books
  t.references :author, foreign_key: true
end
```

The `foreign_key: true` is needed here, otherwise just the reference column is created without a backing constraint. When `foreign_key` is true, an index will be created for the column as well.

This is a maximal example.

```ruby
create_table :books
  t.references :author, index: true, foreign_key: true, type: :uuid, null: false
end
```

It is explicit about the foreign key and index. It specifies a `not null` constraint. It declares the type as `uuid` assuming the `authors` table's primary key is of type `uuid`.

Here is an example with a custom column name.

```ruby
create_table :books
  t.references :written_by, foreign_key: { to_table: :authors }
end
```

Here is adding a reference to an existing table.

```ruby
def up
  add_reference :books, :author, index: true, foreign_key: true
end
```

There are more combinations of these, but I hope there is enough here to be able to iterate to a solution that works for you.


---

# 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/different-ways-to-add-a-foreign-key-reference.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.
