---
title: "Rails 7.1 adds adapter option to disallow foreign keys"
description: "Rails 7.1 adds adapter option to disallow foreign keys"
canonical_url: "https://www.bigbinary.com/blog/rails-7-1-adds-adapter-option-to-disallow-foregin-keys"
markdown_url: "https://www.bigbinary.com/blog/rails-7-1-adds-adapter-option-to-disallow-foregin-keys.md"
---

# Rails 7.1 adds adapter option to disallow foreign keys

Rails 7.1 adds adapter option to disallow foreign keys

- Author: Aditya Bhutani
- Published: February 21, 2023
- Categories: Rails, Rails 7

There are times when an application can choose **not** to use foreign keys. Data
transfer between services is a tricky thing. When we are importing the data, we
need to import the data in the right order if "foreign keys" are enabled. In
such cases one might want to not enforce "foreign keys" and load the data and
then add "foreign keys" constraint back.

Situations like this can be handled using migrations. Using migrations, we can
disable "foreign keys". However, this would mean writing migrations for all the
tables and removing all "foreign keys". This could be cumbersome.

```ruby
def change
  create_table :authors do |t|
    t.string :name
    t.timestamps
  end

  create_table :books do |t|
    t.belongs_to :author, foreign_key: false
    t.datetime :published_at
    t.timestamps
  end
end
```

Rails 7.1 adds an option to database.yml that enables skipping foreign key
constraints usage even if the underlying database supports them and solves the
above issue of writing migrations to disable foreign keys for all the tables.

```yaml
development:
  <<: *default
  database: db/development.sqlite3
  foreign_keys: false
```

Please check out this [pull request](https://github.com/rails/rails/pull/45301)
for more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-7-1-adds-adapter-option-to-disallow-foregin-keys)
