Bundle Exec Rake DB Migrate - sql

I have been getting this error when I run bundle exec rake db:migrate for a basic RoR website. I am a beginner and found similar errors on this site and Treehouse but nothing with the specific second half of this error (from the NOT NULLC onward). I am still not sure how to resolve this - can anyone advise? I am running this on windows.
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "em
ail" varchar(255) DEFAULT '' NOT NULLC:/Sites/code/omrails-master/db/migrate/201
30804201341_add_devise_to_users.rb:5:in `block in up'

I guess it happens because you already have the email column in your users table (a previous migration added it, propably that created the table) and the Devise migration you're running (201
30804201341_add_devise_to_users.rb) is trying to re-add it. Is that the case?
If so, open the migration file which first creates the users table and remove the line that creates the email column (it looks something like t.string :email). Close your Rails server, then do the following.
$ bundle exec rake db:drop
$ bundle exec rake db:create
$ bundle exec rake db:migrate

I came across this problem when first starting out. I always solved it by just reseting the database. You already have a column created so reseting it will probably work.
run
bundle exec rake db:reset
than
bundle exec rake db:create
and finally
bundle exec rake db:migrate

Related

Adding a new Migration File using Rake (in the command line)

I know there's a way to automatically create the barebones of a migration file by entering some rake command in the command line but I can't seem to remember exactly what it is. I know it should look something like this...
rake db:create NAME=table_name
but that keeps failing. I also tried
rake db:create_migration NAME=table_name
but no luck with this either. The first attempt returns "Don't know how to build task 'table_name' and the second says "Don't know how to build task 'migration'
rake isn't the command for this, instead you can either:
manually create the file
run rails generate migration table_name in a rails app
or in my case where I'm not using rails, pliny-generate migration table_name

Is it possible to run test in rails without db:create and db:migrate?

i just want to know and want to try, is it possible run rails test without db:create and db:migrate?
I'm new with ruby on rails (rails 5.2.3). i've tried and it cant run the test, i think fixture already handle the data for dummy. Or maybe i missed some steps?
You still have to create the db test before you can run any test. Try this in terminal:
RAILS_ENV=test bin/rails db:create
RAILS_ENV=test bin/rails db:migrate
then try again with your tests. Hope this helps

Uable to run migration on heroku on a postgre database

I need to add a property/column to a table in a production database(Postgre) on Heroku.com (Rails app) by doing migration.
When I do the migration it looks ok, but when I view the columns on the table it has has not added the column!
My development db is sqlite3 and the production db is postgre
I do the following:
heroku run rails generate migration AddUtc_OffsetToEvents utc_offset:integer RAILS_ENV=production --app app-name-1111
and it returns:
invoke active_record
create db/migrate/20130304070946_add_utc_offset_to_events.rb
And then I run the migration
heroku run rake db:migrate RAILS_ENV=production --app app-name-1111
And then:
heroku restart
When I run
heroku pg:psql HEROKU_POSTGRESQL_GRAY_URL --app app-name-1111
and check the columns of the table:
\d+ events
It still does not have the utc_offset column, and no errors are displyed while doing the previous cmds.
Any ideas or hints?
It looks like you are doing several calls to heroku run
Each time you do heroku run it spins up a completely new dyno with your latest code, and when run is over that dyno is destroyed. So the second heroku run does not have the migration filed created in the first.
Since you are already familiar with psql you can just use ALTER TABLE directly. Otherwise you'll need to check your migration into your code and git push heroku master it to heroku, then run it.
Why not just download the code, add the migration and push the changes ?
After, just run the heroku run rake db:migrate on the app.

db:migrate creates the file db/development.sqlite3

db:migrate creates the file db/development.sqlite3 if the rails app uses sqlite. I prefer to use postgresql for development and production before deploying to heroku.
Part of my database.yml file:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: app-development
username: postgres
password: *******
When I run bundle exec rake db:migrate should I be expecting a file such as db/development.postgresql? Because no file is created in the db directory and neither is an error produced.
$ bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "
users.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table
"users"
-> 0.2060s
== CreateUsers: migrated (0.2070s) ===========================================
Well, postgresql is very very different from sqlite. Try reading this article so you can spot the main differences:
http://saaientist.blogspot.com.br/2007/07/choice-of-databases-or-postgres-vs.html.
The thing about your question is that sqlite work with a database file inside your application folder, and postgres don't work this way. There are plenty of resources on how postgresql work with Rails, and i recommend you to read some and try to understand.

Problem with rake: "development database is not configured"

I am novice rails/terminal user and just did a clean install of Lion + Xcode + Rails. Unlike before (on Snow Leopard), I now get an error running rake db:migrate.
I have cloned my code through git which worked fine and created the database witht the "createdb" command but when I try run "rake db:migrate" in terminal it now comes up with this error:
rake aborted!
development database is not configured
My config/database.yml file looks like below in the development section which is exactly the way it looked before on Snow Leopard where it worked fine, so don't know if the error I am now getting is related to Lion.
development:
adapter: postgresql
database: my_db
username: rasmus
encoding: utf8
pool: 5
Can anyone help, please?
I got the same error and in my case it was because the database.yml was not indented correctly. All the configuration parameters should be indented.
Note, be sure to follow the proper spacing conventions. The database config is whitespace aware. Two spaces per attribute works fine. In the following code, note how each attribute has two spaces. Do not use tabs. If you don't use spaces for attributes, rake will not work and throw the same error.
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: db/production
pool: 5
timeout: 5000
password:
You might also want to look for syntax errors in the file. This is the error that will appear if you have a syntax error in your config/database.yml file and you try to do something like start the local web server or run rake db:migrate.
In my case I had accidentally removed the comment from a line at the top of the file and I was seeing this error since the uncommented line made this an invalid yml file.
Solved!
My "gem install pg" had not been run so basically I was missing the pg gem. After "gem install pg" in terminal everything works fine.
Here's a PEBCAK answer for Googlers - check your Gemfile and make sure you have specified your database adapter gem in the proper group in your Gemfile. I had mine defined for only :production and :staging, and at one point must have manually ran gem install pg on my development machine after switching from mysql. This morning I emptied all of the gems for the app and re-bundle install-ed them, then couldn't figure out why the database wouldn't connect. Moving the pg gem spec outside of any group and running bundle install resolved the problem.
A note to others who land on this question page: be sure that you are running the rake db command correctly, i.e:
rake db:migrate instead of rake db migrate
What worked in my case, having tried all the above when rake db:create failed, was to make sure that my Rakefile was properly configured.
This did the job:
require "sinatra/activerecord/rake"
require 'sinatra/asset_pipeline/task'
namespace :db do
task :load_config do
require "./app"
end
end