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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:17:05+00:00 2026-05-11T22:17:05+00:00

How can I make a ForeignKey refer back to the object itself? I’m trying

  • 0

How can I make a ForeignKey refer back to the object itself? I’m trying :

Alias(MyBaseModel):
    type = models.ForeignKey('self')

a = Alias()
a.type = a
a.save()

But then when I run it :

(1048, "Column 'type_id' cannot be null")

I don’t want the type to be null, I want it to contain its own ID. I have tons of objects, but only 1 loops back to itself, so I really don’t want tot make it null. Ideas?

  • 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-11T22:17:05+00:00Added an answer on May 11, 2026 at 10:17 pm

    The problem is that when you did a.type = a, a did not yet exist in the database, therefore it has no pk.

    One way around this is to save twice, first to save it into the database, referring to a dummy object that’s already in the database, then save it again once you can get it from the database. One problem with this is there’s sort of a chicken and egg problem of actually getting such an object into the database in the first place. I’d deal with this by means of creating a fixture to run each time you use sync-db, so that one such object exists and is valid.

    Another option is to relax the not-null constraint and be diligent about getting them assigned.

    Either way, inserting into such a table will always be a pain because you can’t get the PK to use without first inserting, and you can’t insert without having a key to use in the reference field, regardless of ORM or database or anything.

    an alternative solution is to break the circular dependency and do something like:

    AliasType(django.db.Model):
        pass
    
    Alias(MyBaseModel):
        type = models.ForeignKey(AliasType)
    
    a = Alias()
    a_t = AliasType()
    a_t.save()
    a.type = a_t
    a.save()
    

    You can still get useful information by using django’s very smart accessors,

    AliasType.objects.all()[0].alias_set
    

    which can get you back to the original Alias object without having an explicit link to it.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer <element> <a/> <b/> </element> element[child::a][child::b] this should select all the… May 12, 2026 at 12:22 am
  • Editorial Team
    Editorial Team added an answer I found some nice T4 template I could use. Its… May 12, 2026 at 12:22 am
  • Editorial Team
    Editorial Team added an answer MySQL supports CREATE TABLE IF NOT EXISTS foobar, which you… May 12, 2026 at 12:22 am

Related Questions

How can I make a ForeignKey refer back to the object itself? I'm trying
If I have a table in MySQL which represents a base class, and I
I have two tables: CREATE TABLE [dbo].[Context] ( [Identity] int IDENTITY (1, 1) NOT
I want to connect a single ForeignKey to two different models. For example: I
ModelMultipleChoiceField doesn't select initial choices and I can't make the following fix (link below)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.