1

I've been having a problem which doesn't seem uncommon (I've read a lot of stack overflow pages in the last 2 days) but every solution I've read hasn't worked for me.

I've been following this video tutorial At 6:42 the tutor shows the tables in postgresql and mine don't show up.

When I try rake db:migrate the files migrate no problem. rake doesn't throw up any errors, the relevant .rb files are created in the models folder, my schema.rb looks right. It seems postgresql just isn't reading my schema file.

My problem sounds identical to rake db:migrate doesn't seem to work in production

However typing rake db:migrate RAILS_ENV=production doesn't work. (I've tried this a few times after other rake commands like rake db:rollback STEP=3 in the past few days because I was paranoid it was the solution, this person on ruby forum, has the same problem and is offered the same answer).

This is my first attempt at programming anything and I'm loving the tutorial (and the learning curve this problem has turned out to be) Asking here is pretty much my last resort because I've tried everything I can understand online as a possible solution so please help me! Thanks a lot in advance

3
  • Did you create database using rake db:migrate .??? Commented Feb 1, 2016 at 11:31
  • Try rake db:create RAILS_ENV=production to create the production database. Then rake db:migrate RAILS_ENV=production should work. Commented Feb 1, 2016 at 11:44
  • rake db:migrate will throw an error unless the database exists. @SajjadMurtaza @ReggieB Commented Feb 1, 2016 at 12:24

1 Answer 1

1

There is quite a bit of confusion going on here:

Migrations

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.

Postgres does not read your schema file or migrations - rather migrations run SQL queries against your database. In this case a CREATE TABLE ... query will be run when the migration is run.

Migrations are basically a more maintainable and sane way of doing what was classically done by opening a DB console and running SQL queries.

config/schema.rb is not actually used by the database or ActiveRecord - rather its created when you run migrations as a snapshot of what the database schema should look like. Its just a developer convenience. ActiveRecord gets its mappings by querying the database schema.

Migrations and generators

Migrations do not create model files either - those are generators such as:

rails g model Dude abides:boolean

Which creates a CreateDudes migration and a model at app/models/dude.rb.

In fact migrations are just concerned about altering the DB schema and don't care if the model file exists or not - the models is not actually used until you query the database for records.

ENV vars

RAILS_ENV=production sets a environmental variable.

rake db:migrate RAILS_ENV=test

Is the documented way to run a migration in a different environment. Some obscure shells require the ENV var to prefix the command.

However - if you are running a production server you should set the RAILS_ENV env var permanently - not on invocation! This prevents embarrassing misstakes when someone expected you to have configured the server properly and just ran rails s when restarting the server. See the documentation for your server OS on how to set env vars.

If you are still running the migration and do not see the expected results you most likely have not configured config/database.yml properly - the migrations are running. But not against the database you would expect.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for taking the time to help out. Yes everything else is working fine, I think the database must not be configured correctly as you say. I'm not sure where to go next however because I have configured it exactly as is done in the video tutorial I'm following and also the code in config/database.yml is very very simple, so I can't see where the error would be. My database.yml file looks identical to the tutors in this video at 12:30 youtube.com/watch?v=ZPAA4SE7_p4 (after he corrected the spacing mistake). And my app also deploys on heroku no problem. Any ideas? @max
Heroku is a special case since it actually overwrites the settings in database.yml when you push. You might want to take a look at the official rails guides.
For completeness sake and in case other people have this issue I've found out what went wrong. I'd never installed the pg gem (type install gem pg in rails). I went back over all my previous steps trying to install it threw up an error because I'd part installed a different version of rails. So I uninstalled the rails installer and all associated files and started over fresh. I remembered to gem install pg and everything immediately appeared on the postgresql database, finally!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.