How to resolve db:migrate error SQLite3::SQLException: no such table: - ruby-on-rails

I am trying to use the social network gem inkwell.
I am following the simple instructions here .
I have created a Post, User, Category and Community model using rails generate model
I then configure the models and run
$ rake inkwell:install:migrations
$ rake db:migrate
but end up with the error
SQLite3::SQLException: no such table: posts:
There is a migrate file for this model and I always thought that once you'd created the model then following a migrate it would create a table so I'm a bit confused. Certainly when I rails console Post.all there is no table so this migration isn't happening.

As it is said in the instructions you have to create before the model for users and posts.
So if we assume you have just created the models with a generator, run the migrations for them:
$ rake db:migrate
Creating so the required tables and then:
$ rake inkwell:install:migrations
$ rake db:migrate

Did you try a rake db:reset?
Or delete the database file and:
rake db:create
rake db:migrate

Related

Migration not executed from rspec

I created the following migration script (using bundle exec rails generate migration CreateSequenceStudentId):
class CreateSequenceStudentId < ActiveRecord::Migration[6.0]
def change
# Based on: https://schmijos.medium.com/execute-sql-in-rails-migrations-cdb26f51c683
execute 'CREATE SEQUENCE IF NOT EXISTS student_id;'
end
end
Then executed bundle exec rails db:migrate RAILS_ENV=test and it completed. I can access the sequence using psql student_test -c"select nextval('student_id')". Running psql student_test -c"select * from schema_migrations sm" lists 20201122013457 at the very last. No problems so far.
I then wrote the following spec to access the sequence:
sql = "select nextval('student_id')"
p Student.connection.select_all(sql)
And it failed:
# --- Caused by: ---
# PG::UndefinedTable:
# ERROR: relation "student_id" does not exist
# LINE 1: select nextval('student_id')
And I executed psql student_test -c"select nextval('student_id')" one more time and got:
ERROR: relation "student_id" does not exist
LINE 1: select nextval('student_id')
bundle exec rails db:migrate:status RAILS_ENV=test gives:
Status Migration ID Migration Name
--------------------------------------------------
...
up 20201122013457 Create sequence student
So it indicates the migration executed successfully.
psql student_test -c"select * from schema_migrations sm" gives:
version
----------------
20201122013457
20191209013052
20191220005953
20191225001051
20191225001255
20191225001458
...
The new script was executed first. This smells fishy.
Sounds like the migration ran successfully when using db:migrate but not when running rspec. Appreciate any pointers on what I could be doing wrong.
PS: Based on this solution, I have checked if the script name is unique and it is.
I was able to fix your issue by running exact below commands. I found the answer here.
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate
UPDATE:
I found also the way to fix rails db:reset command, you just need to add line config.active_record.schema_format = :sql in your application.rb so info about sequence will be stored in structure.sql. Please take a look that with default schema dump :ruby info about seqeunce is not stored in schema.rb file.
Important: make sure to firstly create structure.sql with rails db:migrate and then run rails db:reset. After just rails db:migrate I'm still getting the error you want to fix.

How to pass Migrations are pending. To resolve this issue, run in rails

I'm working on api projects in rails 4. I created all models by command rails g model myModel, some action in db/migrate has been created a file for migration db, which I do not use for this migrate.
If I run some controller, example localhost:3000/report/data
I found some error :
"Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=development"
It means that, I've to run rails & migrate for every model that I created in my project.
how can I pass it for all models in rails 4? Thanks in advance.
I think you can skip migration creation using the command
rails g model User --skip-migration
Just run
rake db:reset
and then
rake db:migrate
You should run
bin/rake db:migrate RAILS_ENV="development"

Migrations are pending. Unable to create more models ruby on rails

How to create more models in ruby on rails? at first create one model like this type of command:
$ rails g model user name:string email:string
Create properly, but when I try to create second model like this type of command:
$ rails g model job job1:string job2:string
Not create any model & showing this type of error
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
How can I solve this issue? I'm using Rails 4.2.5, ruby 2.1.7p400 & mysql
As the error you are getting Migrations are pending. To resolve this issue,...
To fix this you need to follow the below step.
1: In terminal you need to Run
rake db:migrate # in order to run the all pending migration task
2: Then
rails g model job job1:string job2:string
Should work!!!
hope this solve your issue!!!
Do a rake db:migrate in your terminal every time you generate a model(s) or make some changes in your model. Only then it will be applied to your rails app.
For more on migration you can visit: http://guides.rubyonrails.org/v3.2/migrations.html
Each time you create a migration or/and add a model (or any changes to your table) you have to run rake db:migrate to apply those changes.
You can also see which migrations are pending by using rake db:migrate:status
Hope it sheds a bit of light on your issue.

rake function in ruby-on-rails

I am very new in ruby-on-rails. I have doubt in rake. create function is create a new db. After that, we want to run some commands like
rake db:load
rake db:data:load
rake db:schema:load
rake db:migrate
rake db:seed
But why we want to run this cmds after create the db and what function of the about cmd.
Thanks for your advice.
You can use rake -T to get the description of each task:
$ rake -T | grep db
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql
rake db:version # Retrieves the current schema version number
I this what you were asking about?
EDIT:
You can read more about what migrations are for here: http://guides.rubyonrails.org/migrations.html
EDIT 2:
rake db:migrate allows you to update the DB schema in a "sane" way: you can create a new migration (read the guides!) and add a new column for example, add an index, rename a column, etc. - migrations allow you to "travel" back and forth in "time" - you can run the migration and rollback it later.
When you generate a new migration:
$ rails g migration add_new_column_to_some_table you will be able to run rake db:migrate later on to apply to changes you want (of course you have to write the body of this generated migration).
I STRONLY advise you to read the guides though :)
EDIT 3:
add_column :users, :price, :float, for example, will add price column to the users table, the type of the column will be float (float isn't a best idea to store money related things BTW!). This column will be NULL by default.
EDIT 4:
Information about which migrations were run is stored in schema_migrations table: running migration for the first time will add a new record in this table with version of this migration (date + some random numbers from the name of the file). Rollbacking a migration will remove this record. Running a migration twice will not have any effect.
Simply put, db:migrate doesn't destroy existing data you have in your database. so running migrations and that rake task allows your data to exist from changes you make.
Workflow in words
You create an empty data base.
You will want to add a table of data for your first model into the database. This is done by editing a file in db/migrate
Now you want a second model, so you create your model and edit the migration file in db/migrate that was created for you
The easiest way to update your database without destroy any of the existing data, is to run bundle exec rake db:migrate. This adds the contents of the second migration file into the db and won't hurt existing data.
Example workflow after creating a project:
bundle exec rake db:create:all
bundle exec rails generate scaffold Users name:string email:string
bundle exec rake db:migrate
At this stage, bundle exec rails s and go to localhost:3000/users/new and create a new user.
bundle exec rails generate scaffold Posts title:string body:text
bundle exec rake db:migrate
Back in your browser go to localhost:3000/users and you should still see the user you created.

Issue creating and applying the migration that adds the director field to the movies table

This is the code I thought is correct:
rails generate migration AddDirectorsToMovies Directors:string
When I run rake db:test:prepare, the terminal tells me:
Run "rake db:migrate" to update your database then try again. The rake aborts.
Did you run rake db:migrate first? That task will generate the schema file, which is then used by rake db:test:prepare.

Resources