JohnD
JohnD

Reputation: 61

Why is Django adding an extra character to the table name of a legacy database?

I am following this_site for onboarding a legacy database. Though, irregardless of the method used Django is adding an extra character (an s) to the only table within a legacy sqlite database (table: Dogs).

Screenshot below of the extra character:

enter image description here

And my entire models.py below.

Accessing the database through python's sqlite library I can query the table 'dogs' and get expected results where as querying 'dogss' yields the error "no such table: dogss".

from django.db import models
from datetime import datetime

# Create your models here.

class Tutorial(models.Model):
    tutorial_title = models.CharField(max_length=200)
    tutorial_content = models.TextField()
    tutorial_published = models.DateTimeField('date published', default=datetime.now())

    def __str__(self):
        return self.tutorial_title

class Dogs(models.Model):
    #id = models.AutoField(primary_key=True)
    date_of_death = models.TextField(db_column='Date of Death', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    land_of_birth = models.TextField(db_column='Land of Birth', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    registered_name = models.TextField(db_column='Registered Name', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    call_name = models.TextField(db_column='Call Name', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    breeder_name = models.TextField(db_column='Breeder_name', blank=True, null=True)  # Field name made lowercase.
    registration_field = models.TextField(db_column='Registration#', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    land_of_standing = models.TextField(db_column='Land of Standing', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    dam = models.TextField(db_column='Dam', blank=True, null=True)  # Field name made lowercase.
    known_offspring = models.TextField(db_column='Known Offspring', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    colour = models.TextField(db_column='Colour', blank=True, null=True)  # Field name made lowercase.
    breeder = models.TextField(db_column='Breeder', blank=True, null=True)  # Field name made lowercase.
    kennel = models.TextField(db_column='Kennel', blank=True, null=True)  # Field name made lowercase.
    owner_name = models.TextField(db_column='Owner_name', blank=True, null=True)  # Field name made lowercase.
    weight = models.TextField(db_column='Weight', blank=True, null=True)  # Field name made lowercase.
    sire = models.TextField(db_column='Sire', blank=True, null=True)  # Field name made lowercase.
    did = models.IntegerField(blank=True, null=True)
    sex = models.TextField(db_column='Sex', blank=True, null=True)  # Field name made lowercase.
    titles = models.TextField(db_column='Titles', blank=True, null=True)  # Field name made lowercase.
    date_of_birth = models.DateTimeField(db_column='Date of Birth', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    owner = models.TextField(db_column='Owner', blank=True, null=True)  # Field name made lowercase.
    distinguishing_features = models.TextField(db_column='Distinguishing Features', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    size = models.TextField(db_column='Size', blank=True, null=True)  # Field name made lowercase.

    def __str__(self):
        return self.registered_name

    class Meta:
        managed = False
        db_table = 'dogs'
        app_label = 'dogs'

Upvotes: 0

Views: 460

Answers (1)

vctrd
vctrd

Reputation: 518

This is the default display in Django without specifying a verbose_name_plural, you have the documentation available here https://docs.djangoproject.com/en/3.0/ref/models/options/

It is standard practice to name a table with a singular name, thus by default Django adds an s to show the list of items present in the table. Here is the discussion regarding this topic: Table Naming Dilemma: Singular vs. Plural Names

(In the case of the Tutorial table you probably have tutorials displayed also)

Upvotes: 1

Related Questions