Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:20:19+00:00 2026-05-31T17:20:19+00:00

I am working on the project and now I need to add a foreign

  • 0

I am working on the project and now I need to add a foreign key. I have these two models:class

class Owner < ActiveRecord::Base
  has_many :shops
end

class Shop < ActiveRecord::Base
  belongs_to :owner
  has_many :cities
end

class City < ActiveRecord::Base
  belongs_to :shop, :foreign_key => 'shop_sid'
end

My goal is print all cities, in which is the shop. I am trying to do that this way:

<% @owner.shops.each do |shop| %>
  <% shop.cities.each do |city| %>
     <%=city.position%><br />
  <%end%>
<%end>

But I am getting the error
PG::Error: ERROR:  column cities.shop_id does not exist
LINE 1: ...T "cities".* FROM "cities"  WHERE "cities...
                                                             ^
: SELECT "cities".* FROM "cities"  WHERE "cities"."shop_id" = 8

I also tried to add the foreign key to the cities table:

alter table cities add foreign key (shop_sid) references shops (sid);

But got

ERROR:  there is no unique constraint matching given keys for referenced table "shops"

How could I solve this problem about foreign key? Or, would be also better to update the associations?

EDIT – error message:

PG::Error: ERROR:  operator does not exist: character varying = integer
LINE 1: ...ons"  WHERE "cities"."shop_sid" = 1034
                                                                 ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "cities".* FROM "cities"  WHERE "cities"."shop_sid" = 1034
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:20:21+00:00Added an answer on May 31, 2026 at 5:20 pm

    You need to specify the foreign key on each side of the relation:

    class Shop < ActiveRecord::Base
      belongs_to :owner
      has_many :cities, :foreign_key => 'shop_sid'
    end
    
    class City < ActiveRecord::Base
      belongs_to :shop, :foreign_key => 'shop_sid'
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have an at-home project now that I'm working on Python/Django at
So I have been working on this project for a short while now. I
I've been working on this project for about two weeks now. I don't understand
I'm working on a project right now where I have been slowly accumulating a
I have been working on a project for a few months now and my
I'm working on an Android project and I need to add buttons to a
I have been working on the project where application is using grails 1.2.2.... Now
I have created a project in IntelliJ, and it's working. Now I want to
I'm working on a small Java project that now connects to a MS SQL
I'm working on a project which I want to build up OO. Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.