logo
down
shadow

Custom unique_together key name


Custom unique_together key name

By : James Holland
Date : November 17 2020, 11:55 AM
it fixes the issue Changing index name in ./manage.py sqlall output.
You could run ./manage.py sqlall yourself and add in the constraint name yourself and apply manually instead of syncdb.
code :
$ ./manage.py sqlall test
BEGIN;
CREATE TABLE `test_mymodel` (
    `CLID` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `CSID` integer NOT NULL,
    `CID` integer NOT NULL,
    `UUID` varchar(96) NOT NULL,
    UNIQUE (`CSID`, `CID`, `UUID`)
)
;

COMMIT;
$ ./manage.py sqlall test
BEGIN;
CREATE TABLE `test_mymodel` (
    `CLID` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `CSID` integer NOT NULL,
    `CID` integer NOT NULL,
    `UUID` varchar(96) NOT NULL,
    UNIQUE constraint_name (`CSID`, `CID`, `UUID`)
)
;

COMMIT;
BEGIN;
CREATE TABLE "testapp_mymodel" (
    "CLID" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "CSID" integer NOT NULL,
    "CID" integer NOT NULL,
    "UUID" varchar(96) NOT NULL,
    UNIQUE ("CSID", "CID", "UUID")
)
;

COMMIT;
class BaseDatabaseSchemaEditor(object):
    # Overrideable SQL templates
    sql_create_table_unique = "UNIQUE (%(columns)s)"
    sql_create_unique = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s UNIQUE (%(columns)s)"
    sql_delete_unique = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"
    # Add any unique_togethers
    for fields in model._meta.unique_together:
        columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
        column_sqls.append(self.sql_create_table_unique % {
            "columns": ", ".join(self.quote_name(column) for column in columns),
        })


Share : facebook icon twitter icon
unique_together on self

unique_together on self


By : maddy
Date : March 29 2020, 07:55 AM
With these it helps The OneToOneField enforces a unique on player, therefore the unique_together is redundant. To make your model work you should change it player to a ForeignKey.
The following models would probably be a little better for your needs:
code :
class Season(Model):
    start_date = DateTimeField(null=True, blank=True)
    end_date = DateTimeField(null=True, blank=True)
    label = CharField(max_length=20)

class PlayerRole(Model):
    season = ForeignKey(Season)
    player = ForeignKey(Player)
    team = ForeignKey(Team)
    role = CharField(max_length=20, choices=PLAYER_ROLES)
    #role can be either "Captain" or "Regular" for a certain team
    class Meta:
        unique_together = ("season", "player")
        # OR, TO ALLOW PLAYERS MOVING BETWEEN TEAM IN SEASON:
        # unique_together = ("season", "player", "team") 
Django unique_together custom error message

Django unique_together custom error message


By : Ray
Date : March 29 2020, 07:55 AM
To fix the issue you can do The error message you pasted seems more like an error caused by a unique constraint on the mobile field of the recipient table, indicating that at one point you had unique=True at one point. Check for this constraint in the database or if this is just in development, delete the database and syncdb again.
How do I use unique_together in django?

How do I use unique_together in django?


By : kkmahen
Date : March 29 2020, 07:55 AM
like below fixes the issue Defining the constraint is quite straight forward. The tricky part for you is to create that one user if you randomly assign group numbers but no matter how you twist and turn it you would have a lot of SQL queries made since you need to check if user already exists.
code :
class Person(models.Model):
    firstname = models.CharField(max_length = 30)
    group = models.IntegerField(max_length = 5)

    class Meta:
        unique_together = ("firstname", "group")
from random import randint
def assign_group_and_save():
    rand_group = randint(1, 99999)
    person, created = Person.objects.get_or_create(firstname=self.firstname, group=rand_group)
    if not created:
        #try again
        return self.assign_group_and_save()
    else:
        return person
unique_together and M2M field

unique_together and M2M field


By : ThOsam
Date : March 29 2020, 07:55 AM
help you fix your problem you can use through in ManyToManyFieldand connect your many to many relations through custom intermediate table. then add unique_together there:
code :
class CodeSynonyms(models.Model):
    websites = models.ManyToManyField(Websites, through='WebsiteCode')
    synonym = models.Charfield(max_length=10)

class WebsiteCode(models.Model):
    code_synonym - models.ForeignKey(CodeSynonyms, on_delete=models.CASCADE)
    website = models.ForeignKey(Websites, on_delete=models.CASCADE)
    code = models.ForeignKey(Codes, on_delete=models.CASCADE)

    class Meta:
        unique together = ('code', 'website')
Django custom user model with unique_together on the email

Django custom user model with unique_together on the email


By : Annihilus
Date : March 29 2020, 07:55 AM
With these it helps Add auth.E003 to the SILENCED_SYSTEM_CHECKS setting. This will allow manage.py to run. And I think you should add W004 warning to this list too:
Related Posts Related Posts :
  • Appending a column in .csv with Python/Pandas
  • How to change my result directory in Robot framework using RIDE?
  • problem with using pandas to manipulate a big text file in python
  • python-magic module' object has no attribute 'open'
  • Where goes wrong for this High Pass Filter in Python?
  • Why inserting keys in order into a python dict is faster than doint it unordered
  • flann index saving in python
  • Create new instance of list or dictionary without class
  • How can I easily convert FORTRAN code to Python code (real code, not wrappers)
  • Address of lambda function in python
  • Python adding space between characters in string. Most efficient way
  • python http server, multiple simultaneous requests
  • Disguising username & password on distributed python scripts
  • Post GraphQL mutation with Python Requests
  • Why doesnt pandas create an excel file?
  • Rolling comparison between a value and a past window, with percentile/quantile
  • How to avoid repetitive code when defining a new type in python with signature verification
  • How to configure uWSGI in order to debug with pdb (--honour-stdin configuration issue)
  • In Python, how do you execute objects that are functions from a list?
  • Python- Variable Won't Subtract?
  • Processing Power In Python
  • Python 2.7.2 - Cannot import name _random or random from sys
  • Why doesn't the Python sorted function take keyword order instead of reverse?
  • Make a function redirect to other functions depending on a variable
  • get_absolute_url in django-categories
  • Monitoring non-Celery background task with New Relic in Python
  • Feature selection with LinearSVC
  • LSTM - Predicting the same constant values after a while
  • Test the length of elements in a list
  • Django: render radiobutton with 3 columns, cost column must change according to size & quantity selected
  • Python class attributes vs global variable
  • sys.stdout.writelines("hello") and sys.stdout.write("hello")
  • is ndarray faster than recarray access?
  • Python - search through directory trees, rename certain files
  • GAE: How to build a query where a string begins with a value
  • TypeError: __init__() takes at least 2 arguments (1 given)
  • Overriding and customizing "django.contrib.auth.views.login"
  • Django : Redirect to a particular page after login
  • Python search and copy files in directory
  • pretty printing numpy ndarrays using unicode characters
  • Frequent pattern mining in Python
  • How can I make a set of functions that can be used synchronously as well as asynchronously?
  • Convert one dice roll to two dice roll
  • count occourrence in a list
  • Writing an If condition to filter out the first word
  • to read file and compare column in python
  • Install python-numpy in the Virtualenv environment
  • `.select_by_visible_text()` is failed to select element?
  • Unable to send data multiple requests in a single connection — socket error
  • Pandas HDFStore unload dataframe from memory
  • Creating a custom admin view
  • How do you get the user role of the currently logged in user in Ckan?
  • Speed up Numpy Meshgrid Command
  • Python error - name lengths
  • appending text to a global variable
  • Python Mistake - Number of letters in name
  • Searching for a sequence in a text
  • Testing logging output with pytest
  • How do I change my default working directory for Python (Anaconda) on VSCode?
  • .lower() for x in list, not working, but works in another scenario
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org