I am using ruby 2.7 and Rails version 6.0.2.1
When I try to test my model I get this message
Error:
OfferTest#test_valid_offer:
DRb::DRbRemoteError: PG::UndefinedTable: ERROR: relation "views" does not exist
LINE 8: WHERE a.attrelid = '"views"'::regclass
^
(ActiveRecord::StatementInvalid)
rails test test/models/offer_test.rb:4
This is my schema file:
ActiveRecord::Schema.define(version: 2020_01_20_105655) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "offers", force: :cascade do |t|
t.string "city"
t.string "area"
t.string "address"
t.string "contact_person"
t.string "contact_person_phone"
t.string "denomination"
t.string "category"
t.string "typology"
t.integer "guests"
t.integer "rooms"
t.boolean "lift"
t.decimal "expense"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "requests", force: :cascade do |t|
t.string "name"
t.string "last_name"
t.string "address"
t.decimal "budget"
t.date "date_of_request"
t.string "document_id"
t.string "phone"
t.string "residential_address"
t.date "date_of_birth"
t.string "notes"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "name", null: false
t.string "last_name", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
Ad of now I have 3 tables. I am testing the Offer model but I get this strange behaviour.
This is my test model code
require 'test_helper'
class OfferTest < ActiveSupport::TestCase
test "valid offer" do
offer = Offer.new(city: "Rome", area: "Zona Sud", address: "Via Roma")
end
end
I've already run rails db:test:prepare but I cannot fix this issue.
My initial thought is that some gem is injecting behaviour into your models.
Something is expecting a table "views". The name views hints at either a gem using database views to virtualize tables, or some gem that works in the domain with views: for example a gem for statistics (An order has been viewed 21 times: has 21 views).
I'd suggest removing all gems from your gemfile and re-including them one by one. This will tell you what gem is injecting this behaviour: knowing what your dependencies do is an important part of building an app, IMO.
If it is a gem, that gem most probably has some migrations that you need to install and run:
bundle exec rake railties:install:migrations
bundle exec rake db:migrate
Related
I have started my project with a Users table and have since migrated to using an Accounts table. In the process I have an old reference to the Users table still in my schema.rb file and I need to remove it and create a new reference, or update the reference.
I am trying to work out a migration that will allow me to do this, however it keeps throwing an error as there's no Users table and when it did exist, it never had an account_id , which you can see referenced in my schema.rb file.
I really just need my schema.rb file to update
"add_foreign_key "likes", "users", column: "account_id"
to
add_foreign_key "likes", "accounts", column: "account_id"
But am finding this impossible to do with a migration without generating an error.
Any suggestions?
ActiveRecord::Schema.define(version: 2022_01_18_013836) do
create_table "accounts", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at", precision: 6
t.datetime "remember_created_at", precision: 6
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "username"
t.string "first_name"
t.string "last_name"
t.index ["email"], name: "index_accounts_on_email", unique: true
t.index ["reset_password_token"], name: "index_accounts_on_reset_password_token", unique: true
end
create_table "likes", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "product_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_likes_on_account_id"
t.index ["product_id"], name: "index_likes_on_product_id"
end
create_table "products", force: :cascade do |t|
t.string "product_name"
t.string "product_category"
t.string "product_type"
t.string "product_image"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.text "product_description"
t.string "product_country"
end
add_foreign_key "likes", "products"
add_foreign_key "likes", "users", column: "account_id"
end
Best way to resolve this is to create a migration that adds users back with only account_id then remove the foreign key, and drop the user table again.
Should be doable in 1 migration, however I went about it as follows.
I ended up creating a new Users table with just an account_id:integer
Created a migration to remove_foreign_key
Then created a migration to then drop that Users table again.
Schema file is looking correct now and I have all the migrations to trace my changes.
I have created a dummy travel agency with Rails 6 and I am trying to use a rake file to seed data for ships, crusies and customer details etc.
The file: ships.rake looks like this:
namespace :ships do
desc "TODO"
task seed_cabins: :environment do
CreditCard.destroy_all
Address.destroy_all
Customer.destroy_all
Cruise.destroy_all
Ship.destroy_all
p "tables emptied"
5.times do |index|
Ship.create!(name: Faker::Coffee.blend_name, tonnage: Faker::Number.within(range: 10000..100000))
end
p "ships created"
# create cabins for each ship
ships = Ship.all
ships.each do |ship|
5.times do |index|
Cabin.create!(
ship_id: ship.id,
name: "Suite #{index+1}",
beds: Faker::Number.between(from: 1, to: 3),
deck: Faker::Number.between(from: 1, to: 3)
)
end
end
p "Cabins created"
ships = Ship.all
ships.each do |ship|
2.times do |index|
Cruise.create!(
ship_id: ship.id,
name: Faker::Hacker.adjective.capitalize + " " +Faker::Hacker.noun.capitalize+" Cruise"
)
end
end
#create customers
3.times do |index |
Customer.create!(
first_name:Faker::Name.first_name,
last_name:Faker::Name.last_name,
has_good_credit: true,
paid: false
)
end
#give each customer an addresses and credit card
customers = Customer.all
customers.each do | customer|
Address.create!(
street:Faker::Address.street_address,
city:Faker::Address.city,
postcode:Faker::Address.postcode,
customer_id: customer.id
)
year = [2020, 2021,2022, 2023]
organisations =["American Express", "MasterCard", "Visa"]
CreditCard.create!(
customer_id:customer.id,
number:Faker::Number.number(12),
exp_date:year.sample.to_s + "/" + Faker::Number.between(1,12).to_s,
name_on_card: customer.first_name + " " + customer.last_name,
organisation: organisations.sample.to_s
)
end
p "customers created"
end
end
The database schema looks like this:
ActiveRecord::Schema.define(version: 2020_10_27_221059) do
create_table "addresses", force: :cascade do |t|
t.string "street"
t.string "city"
t.string "postcode"
t.integer "customer_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["customer_id"], name: "index_addresses_on_customer_id"
end
create_table "cabins", force: :cascade do |t|
t.string "name"
t.integer "beds"
t.integer "deck"
t.integer "ship_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ship_id"], name: "index_cabins_on_ship_id"
end
create_table "credit_cards", force: :cascade do |t|
t.string "number"
t.string "exp_date"
t.string "name_on_card"
t.string "organisation"
t.integer "customer_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["customer_id"], name: "index_credit_cards_on_customer_id"
end
create_table "cruises", force: :cascade do |t|
t.string "name"
t.integer "ship_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ship_id"], name: "index_cruises_on_ship_id"
end
create_table "customers", force: :cascade do |t|
t.string "last_name"
t.string "first_name"
t.integer "has_good_credit"
t.boolean "paid"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "harbours", force: :cascade do |t|
t.string "name"
t.string "country"
t.string "lat"
t.string "long"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "ships", force: :cascade do |t|
t.string "name"
t.integer "tonnage"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "addresses", "customers"
add_foreign_key "cabins", "ships"
add_foreign_key "credit_cards", "customers"
add_foreign_key "cruises", "ships"
end
Essentially, I am just try to run the command: rake ships:seed_cabins and try to generate the data fresh. However, I keep getting the following error:
rake aborted!
ActiveRecord::InvalidForeignKey: SQLite3::ConstraintException: FOREIGN KEY constraint failed
/home/jonathon/Projects/waad/RailsApps/travelagent/lib/tasks/ships.rake:8:in `block (2 levels) in <main>'
Caused by:
SQLite3::ConstraintException: FOREIGN KEY constraint failed
/home/jonathon/Projects/waad/RailsApps/travelagent/lib/tasks/ships.rake:8:in `block (2 levels) in <main>'
Tasks: TOP => ships:seed_cabins
(See full trace by running task with --trace)
I am aware the order of when the tables are destroyed is an issue and the current order is the closest I can get to the file working. it seems to be the Ship.destroy_all line that is the issue because when I remove it and run the file it runs with no issues!
However, as far as I can see there would be no constraints left on that database once the other tables are cleared that would prevent Ship from being deleted?
If anyone could point me in the right direction I would be very grateful.
You missed destroying the cabins, which are still referencing the ships as foreign key. Hence the foreign key constraint is kicking in.
On a side note, you can make your life easier by adding dependent: :destroy in your associations. For example, in your Ship model, you would add,
class Ship < ApplicationRecord
has_many :cabins, dependent: :destroy
end
Since a cabin doesn't make sense without the corresponding ship. What this will do is, whenever a Ship instance is destroyed, it will destroy the related cabins as well.
So I'm working on migrating into a SQLite db on Rails and somehow ended up with User and Users tables. How do I remove it without removing Users?
create_table "user", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "email"
t.string "password"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "email"
t.string "password"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
May not be best practice, but what seems to have solved it for me was to delete from the schema,drop the databaserails db:dropcreate a new onerails db:create and migrate rails db:migrate
Good to go as far as I can tell!
I have seen this question posted several times and the solution is always to drop the database and recreate it. I have data in my database and hence do not want to do that.
Schema:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "product_id"
end
My second to last migration file:
class AddProductIdToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :product_id, :string
end
end
I have no other migration file that creates a product_id column on my current branch.
I have multiple branches with different database schema. I am wondering if that caused the issue. The branch that might have created the product_id is only there for reference now. It will not be merged to master.
How do I fix this issue? I have tried:
rake db:rollback step=3
rake db:migrate
but that did not work.
Your create_table is already creating product_id inside the database.
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "product_id" // <- note this line
end
And you are trying to add another column of same name in your table, which raises an error.
When I run:
rails db:seed
in the command line, I get the following error:
ActiveModel::UnknownAttributeError: unknown attribute 'name' for Review..........................................................
I also get the same error when I run:
rails db:seed
seeds.rb
movie = Movie.find_by(title: 'Iron Man')
movie.reviews.create!(name: "Roger Ebert", stars: 3, comment: "I laughed, I cried, I spilled my popcorn!")
movie.reviews.create!(name: "Gene Siskel", stars: 5, comment: "I'm a better reviewer than he is.")
movie.reviews.create!(name: "Peter Travers", stars: 4, comment: "It's been years since a movie superhero was this fierce and this funny.")
movie = Movie.find_by(title: 'Superman')
movie.reviews.create!(name: "Elvis Mitchell", stars: 5, comment: "It's a bird, it's a plane, it's a blockbuster!")
Genre.create!(name: "Action")
Genre.create!(name: "Comedy")
Genre.create!(name: "Drama")
Genre.create!(name: "Romance")
Genre.create!(name: "Thriller")
Genre.create!(name: "Fantasy")
Genre.create!(name: "Documentary")
Genre.create!(name: "Adventure")
Genre.create!(name: "Animation")
Genre.create!(name: "Sci-Fi")
Here is my schema filee [sic]:
ActiveRecord::Schema.define(version: 20181218222845) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "characterizations", force: :cascade do |t|
t.bigint "movie_id"
t.bigint "genre_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["genre_id"], name: "index_characterizations_on_genre_id"
t.index ["movie_id"], name: "index_characterizations_on_movie_id"
end
create_table "favorites", force: :cascade do |t|
t.bigint "movie_id"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["movie_id"], name: "index_favorites_on_movie_id"
t.index ["user_id"], name: "index_favorites_on_user_id"
end
create_table "genres", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "movies", force: :cascade do |t|
t.string "title"
t.string "rating"
t.decimal "total_gross"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
t.date "released_on"
t.string "cast"
t.string "director"
t.string "duration"
t.string "image_file_name", default: ""
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "main_image"
t.string "slug"
end
create_table "reviews", force: :cascade do |t|
t.integer "stars"
t.text "comment"
t.bigint "movie_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["movie_id"], name: "index_reviews_on_movie_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.boolean "admin", default: false
end
add_foreign_key "characterizations", "genres"
add_foreign_key "characterizations", "movies"
add_foreign_key "favorites", "movies"
add_foreign_key "favorites", "users"
add_foreign_key "reviews", "movies"
end
Your reviews table does not have a name column, which is what Rails is telling you.
I don’t know how your app works, but I do see a user_id column on the reviews table, and the users table has a name column, so I imagine you need to create a user using that name attribute, and then pass the user into the review that you’re creating.
Without seeing your Review and User model, I can’t speak for sure.
At a minimum, you'll need to create a migration & run it to add the name field to your reviews table:
rails generate migration AddNameToReviews name:string
rails db:migrate
This should fix your reported error.