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 578433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:15:35+00:00 2026-05-13T14:15:35+00:00

The geonames database has two models that refer to each other. In a postgres

  • 0

The geonames database has two models that refer to each other. In a postgres database, reference is provided by foreign key relationships. The problem is writing data to the database – writing to one table without having the referenced id in the other table violates a foreign key contraint.

Class Geoname
    id = IntegerField(primary_key=True)
    admin_1 = foreignkey('Admin1', null=True, db_index=True)

Class Admin1
    country = models.ForeignKey(Country, db_index=True)
    geoname = models.ForeignKey(Geoname, related_name="admin1_set", db_index=True)

Is there a way to write to a postgres database with this schema, avoiding violating foreign key contraints?

If not, I think a possible solution might be to drop admin_1 from geoname class, but would django still pick up on geoname.admin1_set? Remember, the data is being written directly to the database without the ORM – i think backwards relations are something that is created by the django ORM – I don’t know how they are represented at the database level).

  • 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-13T14:15:35+00:00Added an answer on May 13, 2026 at 2:15 pm

    First: Having a cyclic relationship in your database schema means a bad design of your schema.

    Backwards reference is indeed created by the ORM, but it is created on the fly (on reading) not on writing.
    Otherwise it would not be possible to load fixtures for automated tests…

    Is it a one-to-one relation? If so, you can use the OneToOneField:

    Class Geoname
        id = IntegerField(primary_key=True)
        admin_1 = OneToOneField('Admin1')
    
    Class Admin1
        country = models.ForeignKey(Country, db_index=True)
    

    I hope these classes inherit from django.db.models.Model in your real code!

    If it is not a one-to-one relation you can do this:

    Class Geoname
        id = IntegerField(primary_key=True)
    
    Class Admin1
        country = models.ForeignKey(Country, db_index=True)
        geoname = models.ForeignKey(Geoname, db_index=True)
    

    and you have access to all Admin1 objects that refer to a certain Geoname by

    g = Geoname.objects.get(id=1)
    g.admin1_set.all() # Returns all Admin1 objects related to Geoname
    

    You can read about such queries in the documenation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I'm using a local copy of the Geonames database, including the country-info data.
Does anyone how to import a geonames.org data into my database? The one i'm
I have been coding around with Google Geocoding until I found out that the
I need to be able to compute differences between two data/time values which represent
I have been querying Geonames for parks per state. Mostly there are under 1000
I have the following jquery which reads from the excellent geonames.org web service: $('#<%=txtFindMyCity.ClientID%>').autocomplete(http://ws.geonames.org/searchJSON,
I am trying to send this query: http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK To a web service and pull
is there any possibility to determine the timezone of point (lat/lon) without using webservices?
I have a function (internally uses ASIHTTPRequest) which calls a block with the result:
I'm working on an auto-complete input field somewhat similar to the one on Facebook

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.