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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:27:14+00:00 2026-05-17T00:27:14+00:00

I want a table which can only have one record. My current solution is:

  • 0

I want a table which can only have one record. My current solution is:

class HitchingPost(models.Model):
    SINGLETON_CHOICES = (('S', 'Singleton'),)
    singleton = models.CharField(max_length=1, choices=SINGLETON_CHOICES, unique=True, null=False, default='S');
    value = models.IntegerField()

    def __unicode__(self):
        return u"HitchingPost" # only ever one record

This is a bit ugly, and doesn’t enforce the constraint at the MySQL level.

Is there a better solution?

Is there a MySQL field type which can only have one value (boolean is the smallest I’ve found, having two possibilities)? A base-0 digit is the nearest I’ve come to expressing the concept.

Is there a mathematical name for such a thing?

Thanks,

Chris.

P.S. Generated SQL is:

CREATE TABLE `appname_hitchingpost` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `singleton` varchar(1) NOT NULL UNIQUE,
    `value` integer NOT NULL
)
;
  • 1 1 Answer
  • 4 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-17T00:27:15+00:00Added an answer on May 17, 2026 at 12:27 am

    This is a bit ugly, and doesn’t enforce the constraint at the MySQL level.

    If you are worried about enforcement you ought to look at Django’s model validation methods. You can write a custom validate_unique that will raise a ValidationError if HitchingPost.objects.count() != 0.

    class HitchingPost(models.Model):
        ...
        def validate_unique(self, exclude = None):
            from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
            if HitchingPost.objects.count() != 0:
                raise ValidationError({NON_FIELD_ERRORS: ["There can be only one!"]})
    

    Is there a better solution?

    Hard to say without getting knowing more about your broader requirement.

    Is there a MySQL field type which can only have one value (boolean is the smallest I’ve found, having two possibilities)? A base-0 digit is the nearest I’ve come to expressing the concept.

    You can try a custom single element enum. I’ve never tried anything like it, so take my advice with a pinch of salt.

    Is there a mathematical name for such a thing?

    Set-once-constant? I made that up. In truth I have no idea. Better people here will help you out.

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

Sidebar

Related Questions

I have a table which i want to store all the outputs into one
I want to find which tables are related to a specific table. I can
i have a table which has many fields but i want to get count
I want to get all the records from the one table which contain at
I have a mysql table which columns are post_id and post_msg. Now I want
I have this JSF table which is used to display data. I want to
my table have several records which has the same MemberID. i want to result
I have a dilemma, maybe you can help me. I have a table which
I am having comment reply (only till one level) functionality. All comments can have
I want to join a table which contains n:m relationship between groups. (Groups are

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.