Issue regarding running rake db:seed task - ruby-on-rails

I have an existing rails project and I am using postgres database and after successful execution of bundle exec rake db:migrate command when I am going to execute the bundle exec rake db:seed command then it throws me the following error.
rake aborted!
PG::DuplicateTable: ERROR: relation "schema_migrations" already exists
I have commented out the lines where the definition of the schema_migration table creation is stored such as in the development_structure.sql and test_structure.sql files. But it is still throwing me the error.
I don't know from where the schema_migration table is being created. There is no schema_migration.rb file in my project.
Pleas help me to fix this. Thanks in advance!
This is my error log.
rake aborted!
PG::DuplicateTable: ERROR: relation "schema_migrations" already exists
: CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `rescue in log'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log'
/home/bit/Development/app2/application/lib/core_extensions/postgresql_adapter/reconnect.rb:26:in `execute'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:109:in `create_table'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:371:in `initialize_schema_migrations_table'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/migration.rb:441:in `initialize'
/home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `new'
/home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `block (2 levels) in <top (required)>'
/home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
/home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed => db:seed:common => db:seed:original => db:abort_if_pending_migrations
(See full trace by running task with --trace)

If it's OK to drop your data locally, try
rake db:drop db:create db:migrate
and then seed again. I've had similar issues in the past where I deleted a migration without properly rolling it back and this was the solution.
If you need to keep it, open the rails database (rails db from terminal) and try
drop table schema_migrations
(Run the migrations with db:migrate again after this)
Edit: This has not solved the issue yet, but it seems there may be other issues that need to be factored in. OP cannot launch rails console and receives
undefined local variable or method `vars' for #<Rails::Generator::Commands::Create:0x00000001ca2578>
as the error message each time - if anyone has ideas here, please add an answer!

Related

Pre-existing database with new rails application

I have an pre-existing database named exam_prep in postgresql, which i am using in flask.
I have created a new Rails application and i want to use that database with it.
So far I have run rake db:structure:dump, which created a structure.sql under the db folder.
After that I ran db:structure:load, but got the following error:
psql:/Users/namangupta/Desktop/app/db/structure.sql:49: ERROR: type "batch_types_enum" already exists
rake aborted!
failed to execute:
psql -v ON_ERROR_STOP=1 -q -f /Users/namangupta/Desktop/app/db/structure.sql exam_prep
Please check the output above for any errors and make sure that `psql` is installed in your PATH and has proper permissions.
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:108:in `run_cmd'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:80:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:223:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:236:in `load_schema'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:255:in `block in load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:254:in `load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:290:in `block (3 levels) in <top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `eval'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:load
(See full trace by running task with --trace)
I want Rails to use the database and make a models within this. Any ideas?
The db:structure:load rake task creates a tables and columns from the structure.sql or schema.rb. You shouldn't need to run it, as your existing database already has all the tables you need.
Your next step would be to go about creating your model files and pointing them at the appropriate tables.

rails can't migrate: "don't know how to build task VERSION:0"

First post on here so apologies for any noobity.
I'm working through the Lynda.com Ruby on Rails 5 Essential training on C9 and encountered a problem trying to migrate back down to 'VERSION:0' with this error message:
rails aborted!
Don't know how to build task 'VERSION:0' (see --tasks)
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/commands/rake/rake_command.rb:21:in block in perform'
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/commands/rake/rake_command.rb:18:inperform'
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/command.rb:46:in invoke'
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/commands.rb:16:in'
/home/ubuntu/workspace/bin/rails:9:in require'
/home/ubuntu/workspace/bin/rails:9:in'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in load'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:incall'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in call'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client.rb:30:inrun'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/bin/spring:49:in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:inload'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in <top (required)>'
/home/ubuntu/workspace/bin/spring:15:inrequire'
/home/ubuntu/workspace/bin/spring:15:in <top (required)>'
bin/rails:3:inload'
bin/rails:3:in `'
Any help or advice much appreciated
When doing migration in Rails, you can do:
rake db:migrate, which will migrate your new migration to database and add them to scheme.rb.
You can also do rake db:rollback, which will rollback your last migration.
For rollback you can also do rake db:rollback STEP=n, which will roll you back n migrations where n is the number of recent migrations you want to rollback.
IF you want to rollback version of your migration you would apply the migration version as: rake db:migrate VERSION=0 (as #sjudge said in comments)
You can also do rake db:migrate:redo.
There also some other migrations like:
rake db:migrate:redo STEP=n
rake db:migrate:up VERSION=20080906120000
rake db:migrate:down VERSION=20080906120000

Starting PostgreSQL database for Rails app

I'm trying to get my Rails app connected to a PostgreSQL database.
I successfully ran rake db:create
Now when I run rake db:migrate I get the following error ...
===============================================================================
HINT:
You might not have the 'pgcrypto' PostgreSQL extension installed.
Install using:
CREATE extension IF NOT EXISTS pgcrypto SCHEMA pg_catalog;
You can also install it in the 'template1' database for future databases.
===============================================================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::UndefinedFunction: ERROR: function gen_salt(unknown) does not exist
LINE 3: (SELECT gen_salt('bf') AS salt)
^
I tried installing the pgcrypto extension, but I get the same error.
I also tried running rake db:setup and I get this ...
============================================================
WARNING: Pulse calls are disabled until the application is restarted.
rake aborted!
No such file or directory # rb_sysopen - /Users/kweihe/pmpaware-webapp/db/structure.sql
/Users/kweihe/pmpaware-webapp/lib/tasks/db.rake:8:in `initialize'
/Users/kweihe/pmpaware-webapp/lib/tasks/db.rake:8:in `new'
/Users/kweihe/pmpaware-webapp/lib/tasks/db.rake:8:in `block (3 levels) in <top (required)>'
/Users/kweihe/.rvm/gems/ruby-2.1.6/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:484:in `block (3 levels) in <top (required)>'
/Users/kweihe/.rvm/gems/ruby-2.1.6/bin/ruby_executable_hooks:15:in `eval'
/Users/kweihe/.rvm/gems/ruby-2.1.6/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:load => db:structure:pg_crypto_preflight
(See full trace by running task with --trace)
FIXED
I installed the extension in the wrong db. Went through PgAdmin and installed in correct db, now migrate runs successfully
I installed the extension in the wrong db. Went through PgAdmin and installed in correct db, now migrate runs successfully
Installed extension by running this in the query field:
CREATE extension IF NOT EXISTS pgcrypto SCHEMA pg_catalog;

rake db:migrate:reset to dump schema

Hell, I'm following in Hartl's Ruby on Rails tutorial and I'm on chapter 9 adding administrative control to users.
I'm at the part where it says
reset the database:
$ bundle exec rake db:migrate:reset
$ bundle exec rake db:seed
Last time I followed this (much earlier in the tutorial), it reset the database as instructed but also deleted me schema.rb. Which was annoying.
Attempted it again, and yet again it deleted my database and erased my schema.rb. Surely this is not intended.
I'm having trouble figuring out what I should be doing anymore. I'm now getting all kinds of errors with my database. I get errors about pending migrations, but no db:xxx command works, every command just spits out this long list of errors such as
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
What does one do in such a situation?
try to verify following :-
you have the db with username/password mentioned in database.yml
you have db empty
when you run rake db:migrate,then schema.rb is getting generated
you have a valid database.yml file

how to use seed-Fu gem in ruby

I'm trying to work with "seed-fu" gem.I follow the document here. From that I didn't find the fixtures folder in Rails-ROOT/db/.My db folder contain developement.sqlite3, seeds,rb file.
For that I manually created fixtures under db/fixtures/users.rb.
When giving the command as "rake db:seed_fu". It shows the following in my prompt as,
== Seed from D:RAILS_ROOT/db/fixtures/users.rb
rake aborted!
uninitialized constant SeedFu::Runner::User
Tasks: TOP => db:seed_fu
(See full trace by running task with --trace)
Whats going on here..?Why I am getting this?
When running "rake db:seed_fu --trace" IT shows the following..,
** Invoke db:seed_fu (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:seed_fu
== Seed from /home/ec2-user/malar/app/db/fixtures/user.rb
rake aborted!
uninitialized constant SeedFu::Runner::User
/home/ec2-user/.rvm/gems/ruby-1.9.2-p290#a-ms/gems/seed-fu-2.2.0/lib/seed-fu/run
ner.rb:46:in `eval'
/home/ec2-user/.rvm/gems/ruby-1.9.2-p290#a-ms/gems/seed-fu-2.2.0/lib/seed-fu/run
ner.rb:46:in `eval'
/home/ec2-user/.rvm/gems/ruby-1.9.2-p290#a-ms/gems/seed-fu-2.2.0/lib/seed-fu/run
ner.rb:46:in `block (2 levels) in run_file'
/home/ec2-user/.rvm/gems/ruby-1.9.2-p290#a-ms/gems/seed-fu-2.2.0/lib/seed-fu/run
ner.rb:58:in `block in open'
/home/ec2-user/.rvm/gems/ruby-1.9.2-p290#a-ms/gems/seed-fu-2.2.0/lib/seed-fu/run
ner.rb:57:in `open'
/home/ec2-user/.rvm/gems/ruby-1.9.2-p290#a-ms/gems/seed-fu-2.2.0/lib/seed-fu/run
ner.rb:57:in `open'
From the presented details, I can't find out what's wrong with your setup but here's a screencast showing the right way to use it:
http://railscasts.com/episodes/179-seed-data
Make sure the models and the name of the seed data match. The seed file can be named whatever you like, but the seed data needs to be the same. See this blog post for an example of using Seed Fu.

Resources