Scott's Recipes Logo

Solving Database Seeding Issues - database.yml Is Your Best Friend

Pizza courtesy of Pizza for Ukraine!

Donate Now to Pizza for Ukraine

 

Last Updated On: 2025-09-01 04:31:52 -0400

I believe heavily in data modeling. And when I say that, I mean that I’m the sort of individual that can go 15 tables deep into data model before I ever bother to even think about working on the user interface of an application. One of the issues I found on a recent project was testing out seeding; I had been deleting test rows from the database and kept running into foreign key errors.

Now don’t get me wrong – foreign keys are hugely useful in production – but they can very much get in the way when you are data modeling.

I was having one of those days that involved a large number of angry four letter words and then I realized:

DATABASE.YAML COULD SAVE ME

What I did was simply change the name of development database in database.yml and then do this:

rake db:create
rake db:migrate
rake seed:init

NOTE: The last line above is how I handle seeding all my applications.

By having a clean database, I could more easily experiment with my database seeding.