Rails3 ActiveRecord::StatementInvalid:... no such table in every test
It would be worth checking that your test database is set up correctly using the schema from your development database. You can achieve this with:
rake db:test:prepare
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table
The scaffold most probably generated a migration with a foreign_key: true
on the references.
Check create_appointments migration file in db/migrate/
and remove it.
If you want to have a foreign key add:
add_foreign_key :appointments, :users, column: :doctor_id
add_foreign_key :appointments, :users, column: :patient_id
https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_foreign_keyThe foreign key constraint checks if the id that you are setting is present in the other table. If it is not, it will crash when you create a record.
rake aborted! ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: Trouble Getting Rake Automation to Work
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: items: SELECT "items".* FROM "items"
From the error message, it's obvious that the items
table does not exist in your database right now. It got deleted somehow even if you did not delete it knowingly. So, you should create the items
table again by running the corresponding migration:
bundle exec rake db:migrate
Update
Check theRAILS_ENV
in which you are running this rake task. As the error is showing up, this means you don't have the items
table present in your database (looks like it's your production database). Then, you have to run the migration in that RAILS_ENV
to create the items
table back:RAILS_ENV=production bundle exec rake db:migrate
SQLite3::SQLException: no such table
I think you can do something like this:
t.references :host, references: :users, foreign_key: true
or alternativelyt.integer :host_id
and thenadd_foreign_key :reviews, :users, column: :host_id
ActiveRecord::StatementInvalid - SQLite3::SQLException: no such column
Well, the SQL problem is that the table name on your sort column is singular rather than plural.
i.e. "ORDER BY micropost.created_at" should read "ORDER BY microposts.created_at".
If you also post your Micropost and User model source code, I might be able to tell if this is caused by a bug in your model code or a bug in ActiveRecord.
Which version of Rails are you using?
How to fix SQLite3::SQLException: no such column: {table_name}.{singular_table_name}_id:
I found out the solution to my issue. In the Country
model, I had a typo that said
class Country < ApplicationRecord
has_many :countries, dependent: :destroy
validates :code, presence: true, uniqueness: true
end
Which is why I was getting the Sqlite3 exception error; because the database assumed I should have a country_id
since country
has many countries
, I think. Anyways, I changed countries
above to cities
, and it worked like a charm. Thanks for the help, SOS. SQLite3 error when testing Cancan authorizations in Rails3 : no such table: abilities: DELETE FROM abilities WHERE 1=1
Problem solved: I had an abilities.yml fixture file. After deleting it, the error is gone.
http://github.com/ryanb/cancan/issues/closed#issue/109
Related Topics
Does Rails Support a Neat Way of Listening to a Udp Socket
Gem Install Pg for Ruby on Rails
Undefined Method 'Merge' for '####':String <%= Form_For %> Helper
How to Add a User Interrupt to an Infinite Loop
Ruby Interpreter (Cui) 1.9.2P180 [I386-Mingw32] Has Stopped Working (I Am Not Using MySQL )
Rake Cucumber and Rake Spec Always Use "Develop" Environment
Remove Adjacent Identical Elements in a Ruby Array
Crontab Not Running Ruby Script
What Does Bundle Install -Without Production Do
How to Reference a Local Gem from a Ruby Script
Ruby: How to Convert an Array of Data to Hash and to JSON Format
Problems While Making a Generic Model in Ruby on Rails 3
Ruby: How to Escape Url with Square Brackets [ and ]
How to Troubleshoot Memory Bloat at Boot for Rails App
Rails/Postgres, 'Foreign Keys' Stored in Array to Create 1-Many Association