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

Related Questions

I'm trying to create a Django app where the user can make a list
How can make a link within a facebox window that redirects it to another
I can make a single row IKImageBrowserView by setting the [imageBrowser setContentResizingMask:NSViewWidthSizable]; but in
I can make a variable's name from two variables' value: $a = 'tea'; $b
I can make a log in for easily, so that's not the problem. What
I can make batch or vbs file on windows and run. this can automate
Can someone tell me how I can make a table be 100% height in
Can someone tell me how I can make the following output? I have a
Just wondering how I can make my app open automatically at login, but make
How I can make a Makefile, because it's the best way when you distribute

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.