简体   繁体   中英

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation table does not exist)

I have 3 tables (Freebie, Company, FreebieCompany) and FreebieCompany is the intermediary table. 2 tables (Freebie, Company) use has_many association and is being connected by the middle table. I can access the Company table to get the freebies but not Freebies to Companies.

class Company < ApplicationRecord
   has_many :freebie_companies
   has_many :freebies, through: :freebie_companies
end
class Freebie < ApplicationRecord
   has_many :freebie_companies
   has_many :companies, through: :freebie_companies
class FreebieCompany < ApplicationRecord
   belongs_to :freebie
   belongs_to :company
end

FreebieCompany have both company_id and freebie_id. I can access the Freebies of a certain company, but not the Companies of a certain Freebie.

I did rake db:reset, drop, create, migrate, setup and db:schema:load but nothing really solved the problem. I double checked the migration version and all tables are listed on the schema.

Maybe another perspective can help me find the solution to this. Every time I try to access the companies, it gives me the error below.

> freebie.companies

* ``` ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "freebie_companies" does not exist) LINE 1: ...ompanies". FROM "companies" INNER JOIN "freebi...


It's my first time to post here, and I've been using my whole day to find the answer to this one.

Any help is very appreciated. Thank you so much!

It really looks like you don't have the association represented in your database. It would be good to see your schema to be sure that your models are being referenced correctly in your DB.

Usually when generating associated models, we run:

rails g model FreebieCompany freebie:references company:references .

This will generate two columns for each association in the FreebieCompanies table.

t.bigint "freebie_id", null: false
t.index ["freebie_id"], name: "index_freebie_companies_on_freebie_id
t.bigint "company_id", null: false
t.index ["company_id"], name: "index_freebie_companies_on_company_id

Here you can find the doc about has_many:through

I forgot to mention that I use multiple databases. Primary and Superadmin Database.

The company table belongs to Primary database, and the association was not automatically presumed. According to the documentation, it needs to add the source to Freebie table to clear the association.

has_many through: source: documentation

Here are my new models:

Intermediary Table:

class FreebieCompany < SuperadminsRecord
 belongs_to :company
 belongs_to :freebie
end

Main Tables:

class Freebie < SuperadminsRecord
 has_many :freebie_companies
 has_many :companies, through: :freebie_companies, source: "freebie"
end

class Company < ApplicationRecord
 has_many :freebie_companies
 has_many :freebies, through: :freebie_companies
end

For the migration file for inter. table:

class CreateFreebieCompanies < ActiveRecord::Migration[6.0]
 def change
  create_table :create_freebie_companies do |t|
   t.references :company, foreign_key: {to_table: :freebies}
   t.integer :freebie_id

   t.timestamps null: false
  end
 end
end

Feel free to clarify and voice out your opinions or concerns to make this one better. I hope this helps. Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM