# Create A Join Table With The Migration DSL

The Rails migration DSL comes with a helper for creating a join table between two other existing tables.

Call `create_join_table` with two arguments, symbols for the names of the two tables.

```ruby
def change
  create_join_table :tags, :posts
end
```

This will create a table with id references columns to each of the tables. The `db/schema.rb` addition will look something like this:

```ruby
  create_table "posts_tags", id: false, force: :cascade do |t|
    t.bigint "tag_id", null: false
    t.bigint "post_id", null: false
  end
```

A Rails/ActiveRecord convention that comes into play for the creation of this table.

1. The name should be the pluralized versions of the two joined table names.
2. The joined table names should show up in the table name in alphabetical order.

Notice that despite listing `:tags` before `:posts` it creates a table called `posts_tags`. The DSL handles that for us.

Some `create_join_table` defaults to be aware of:

* It doesn't generate foreign key constraints.
* It uses `bigint` (or `int`) for the keys (even if those tables use UUIDs).
* The references are named after their respective table names.


---

# Agent Instructions: 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/create-a-join-table-with-the-migration-dsl.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.
