In a multi-database Rails 8.1 app, what does the seeds: key in config/database.yml control, and what is its default?
answer
- read by db:prepare only
- true for the primary database
- false for cache, queue and cable
- loads the whole db/seeds.rb
- db:seed and db:setup ignore it
basics
~10 sseeds: decides whether bin/rails db:prepare runs db/seeds.rb after it sets up that database. It defaults to true for the primary database and false for the others, such as the cache, queue and cable databases.
solid answer
~40 sEach database config answers `seeds?`: the `seeds:` value if the key is present, otherwise `true` for the environment's primary database and `false` for the rest. Only `db:prepare` reads it. When `db:prepare` creates a database or loads its schema, it notes whether that config wants seeds, and after migrating it runs `db/seeds.rb` once if any newly set-up database did. So in production with `primary`, `cache`, `queue` and `cable`, a fresh primary triggers the seeds, while a fresh queue database alone does not. Setting `seeds: true` on another database does not give it seeds of its own: it makes its setup trigger the same whole `db/seeds.rb`. `seeds: false` on the primary, with the others at their default, stops `db:prepare` from seeding at all. `db:seed`, `db:setup` and `db:reset` ignore the key and always load the file.
code
yaml · 9 linesproduction:
primary:
<<: *default
database: lingo_production
seeds: false # db:prepare never seeds automatically
queue:
<<: *default
database: lingo_production_queue
migrations_paths: db/queue_migratego deeper
Know that config/database.yml can carry a seeds: key and that by default only the primary database triggers seeding on first setup.
Explain how db:prepare uses it: seed once if any database it just initialized has seeds? true, with the primary true and others false by default.
Use the key deliberately, for example seeds: false on the primary so production seeding is explicit, and keep the seeds file idempotent for late-added databases.
Decide whether production databases are ever seeded automatically, and how that choice fits the deploy pipeline and recovery from an empty database.
## Why the key exists A Rails 8.1 app often has more than one database. The generated production config already declares four: `primary` for the app's own tables and `cache`, `queue` and `cable` for Solid Cache, Solid Queue and Solid Cable, each with its own `migrations_paths`. `bin/rails db:prepare` sets up whichever of them is missing, and it needs a rule for when to run `db/seeds.rb`. The `seeds:` key is that rule. ## The default Each database configuration answers `seeds?` like this: - if the config has a `seeds:` key, its value; - otherwise **`true` for the primary database** of the environment and **`false` for every other one**. For the language-learning app's production config that means: | Database | `seeds:` set? | `seeds?` | |---|---|---| | `primary` | no | `true` | | `cache` | no | `false` | | `queue` | no | `false` | | `cable` | no | `false` | ## How db:prepare uses it `db:prepare` goes through the environment's databases in three phases: 1. **Initialize.** For each database it checks whether the `schema_migrations` table exists. If not, it creates the database if needed and loads its schema file. If that database was just initialized **and** its config says `seeds?`, it sets a single flag. 2. **Migrate.** It runs pending migrations for every database and dumps the schemas. 3. **Seed.** If the flag is set, it calls `load_seed` **once**, which loads `db/seeds.rb`. The consequences: - A first deploy with an empty primary database seeds, because the primary was initialized. - Adding a new `queue` database to an app that already has a populated primary does **not** seed: only the queue database was initialized, and its `seeds?` is `false`. - Setting **`seeds: true`** on the queue database means its initialization runs the **whole** `db/seeds.rb`. There is no per-database seeds file; the seeds code decides which models and databases it writes to. - Setting **`seeds: false`** on the primary stops `db:prepare` from seeding anything on first setup, a choice some teams make so production seeding is always an explicit `bin/rails db:seed`. ## Tasks that ignore the key The key is about automatic seeding by `db:prepare` only: - `bin/rails db:seed` always loads `db/seeds.rb`; - `bin/rails db:setup` and `db:reset` always seed after loading schemas; - `bin/rails db:seed:replant` always seeds after truncating. ## A worked example ```yaml production: primary: <<: *default database: lingo_production seeds: false queue: <<: *default database: lingo_production_queue migrations_paths: db/queue_migrate ``` With this config, the first boot of a container whose entrypoint runs `db:prepare` creates both databases and loads both schemas, but runs no seeds, because neither config asks for them. Whoever deploys then runs `bin/rails db:seed` on purpose. Without `seeds: false`, the same first boot would load `db/seeds.rb` automatically. ## Checking and setting the value From a console you can ask a configuration directly: ```ruby ActiveRecord::Base.configurations .configs_for(env_name: "production", name: "queue") .seeds? # => false ``` The key can also arrive through a database URL. A URL-based configuration converts a `seeds` query parameter to a boolean, so `?seeds=false` on the primary's `DATABASE_URL` behaves like `seeds: false` in the YAML. That is useful when the platform injects connection strings and you do not want to edit `database.yml` per environment. ## When it matters Most single-database apps never touch the key: the primary seeds on first setup and that is what they want. It becomes relevant when a team either wants seeding in production to be a deliberate step, or adds a database whose first setup should run the seeds. In both cases, read the key together with the rule that seeds must be idempotent, because a database set up later may cause `db/seeds.rb` to run against a primary that is already full.
- A team adds a new analytics database with seeds: true to an app whose primary is already populated. What happens on the next db:prepare?`db:prepare` creates the analytics database and loads its schema, sees that the newly set-up config wants seeds, and runs the whole `db/seeds.rb` once against the app, whose primary already holds data. With an idempotent seeds file that is harmless; with `create!` calls it duplicates or fails.
- seeds: true on the queue database loads a separate queue seeds file
- db:seed skips databases whose seeds: key is false
- every database seeds by default, the primary included
- db:prepare reseeds whenever db/seeds.rb changes
Assumes Rails 8.1, whose generated production database.yml declares primary, cache, queue and cable databases; seeds? defaults to true only for the primary.