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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:26:36+00:00 2026-05-20T09:26:36+00:00

I having a problem getting a ForeignKey to default to zero, it keeps telling

  • 0

I having a problem getting a ForeignKey to default to zero, it keeps telling me that the field is required, or if I try a different iteration it’ll complain the field cannot be null.

I am not sure what I am doing wrong, but I need the ForeignKey to be defaulted to zero if its null.

I am experimenting with an idea to hold all contracts between several different entities (ie: companies and employees, companies and sponsors, etc, companies and suppliers, etc).

The relationships are all working, however my ForeignKey statements are not letting me use a default of 0 (zero).

# relationships
# Only the company field should be required, the others are optional
# This is so you can link a company with either a sponsor OR a worker
#
sponsor     = models.ForeignKey('Sponsor', null=True, default=0, blank=False)
company     = models.ForeignKey('Company')
worker      = models.ForeignKey('Worker', null=False, default=0, blank=False)

# possible future relationships (not implemented)
#suppliers       = models.ForeignKey('Supplier', null=True, default=0, blank=False)

Ideally, I would like it so that only the company field is required, and the others are purely optional and will default to zero.

The idea is to have 1 table to rule/manage all the contracts between different entities.

Pseduocode

Contracts;
id = 1 | company_id = 1 | sponsor_id = 0 | worker_id = 1 | length = 10 | salary = 10000

// etc

I have tried the following iterations.

# models.ForeignKey('Sponsor', null=False, default=0, blank=True) # This makes the field required

# models.ForeignKey('Sponsor', null=True, default=0, blank=True) # This complains that "Contracts.sponsor" does not allow null values. 

Any pointers on how to make a make a ForeignKey use a default would be greatly appreciated.

Many thanks.

Edit. Adding the code that is causing the errors; This is a cut-down version of the code.

// company.py
class Company(models.Model):
    """(Company description)"""

    sponsorContracts = models.ManyToManyField('Sponsor', through='Contracts', db_column='sponsor_id')
    employees   = models.ManyToManyField('Worker', through='Contracts', db_column='worker_id')

// contracts.py
class Contracts(models.Model):
    """(Contracts description)"""
        # relationships
    sponsor             = models.ForeignKey('Sponsor', null=True, default=0, blank=True)
    company             = models.ForeignKey('Company')
    worker              = models.ForeignKey('Worker', null=False, default=0, blank=False)

// worker.py
class Worker(models.Model):
    """(A list of employees)"""

    employer            = models.ManyToManyField('Company', through='Contracts', db_column='company_id')


// sponsor.py
class Sponsor(models.Model):
    """(A list of sponsors)"""

    sponsorContracts    = models.ManyToManyField('Company', through='SponsorContracts')
  • 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-20T09:26:36+00:00Added an answer on May 20, 2026 at 9:26 am

    A relationship between entities either exists (valid reference to id in another table), or it doesn’t (value undefined – null). If you truly ‘need’ a non-null value in there that doesn’t reference a valid entity, you could try having a ‘default’ entity defined in the referenced table, and define the default value to that.

    I’d question the validity of requiring a non-null default FK value however in most cases. Though I have done something similar in one or two cases:

    User(id, username, gender_id)
    Gender(id, value)
    
    Gender:
    id 1 value unknown
    id 2 value male
    id 3 value female
    

    Really though, null is known as value undefined and should be used as such in the majority of cases.

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

Sidebar

Related Questions

Having a problem getting a TreeView control to display node images. The code below
I am having a problem getting a list of fields from a query defined
I'm having a problem getting access to a database which lives on a remote
I am having a problem getting my project to link with the PhysX libraries
After having solved the problem of getting the editor to launch when there's a
I'm trying to query XML through XPATH but is having problem getting id() to
I am having a problem getting the ListView to display properly. It currently looks
I am having a problem getting the FormView Control in Visual Studio 2010 inserting
I am having a problem getting Automatic Differentiation to work between C# and F#.
I'm having a little problem getting my data to submit. I'm trying to make

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.