Opened 7 years ago

Closed 7 years ago

#7236 closed (worksforme)

Table containing ForeignKey created with wrong casing

Reported by: narendra.tumkur@… Owned by: gav
Component: Database layer (models, ORM) Version: master
Severity: Keywords: mysql case table name lower_case_table_names
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by ramiro)

Here is the models.py:

class Plugin(models.Model):
	name = models.CharField(max_length=50)
	description = models.CharField(max_length=100)

class PluginContainer(models.Model):
	title = models.CharField(max_length=50)
	plugin_settings = models.CharField(max_length=2000)
	plugin = models.ForeignKey(Plugin)

PluginContainer refers to Plugin object (ForeignKey)
Running manage.py syncdb produces the tables:

App_plugin
app_plugincontainer

notice the incorrect casing on app_plugincontainer.
Removing the ForeignKey from PluginContainer correctly creates the table as App_plugincontainer

This is causing a problem with running manage.py syncdb again as it cannot find App_plugincontainer and then tries to create app_plugincontainer and fails as the table already exists.

My environment:
OS: Mac OS X 10.5
DB: MySql 5.05
Python: 2.5
Django: SVN

MySql's lower_case_table_names variable is set to 2 (default on mac os x)

Cheers
Naren

Change History (7)

comment:1 Changed 7 years ago by Narendra Tumkur <narendra.tumkur@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Removing this ticket while I enquire in Django users

comment:2 Changed 7 years ago by Narendra Tumkur <narendra.tumkur@…>

  • Resolution invalid deleted
  • Status changed from closed to reopened

Unable to get any response on user groups.
Added some debug statements to syncdb.py and found that the SQL statement being executed did indeed have the correct casing for the table name. However, once again, this problem happens ONLY when the model has a foreign key field. This leads me to believe this is in fact a bug within django and not MySQLdb.

comment:3 Changed 7 years ago by Narendra Tumkur <narendra.tumkur@…>

  • Component changed from Uncategorized to Database wrapper

comment:4 Changed 7 years ago by gav

  • Owner changed from nobody to gav
  • Status changed from reopened to new

comment:5 Changed 7 years ago by ramiro

  • Description modified (diff)
  • Keywords mysql case table name lower_case_table_names added

comment:6 Changed 7 years ago by gav

I have been unable to validate any part of this ticket's request. I have created an example as follows, with an app of the name "MonkeyTest", capitalized just as seen here.

#models.py contents
class Plugin(models.Model):
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=100)


class PluginContainer(models.Model):
    title = models.CharField(max_length=50)
    plugin_settings = models.CharField(max_length=2000)
    plugin = models.ForeignKey(Plugin)

The results of python manage.py sql appear correct:

BEGIN;
CREATE TABLE `MonkeyTest_plugin` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(50) NOT NULL,
    `description` varchar(100) NOT NULL
)
;
CREATE TABLE `MonkeyTest_plugincontainer` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `title` varchar(50) NOT NULL,
    `plugin_settings` varchar(2000) NOT NULL,
    `plugin_id` integer NOT NULL
)
;
ALTER TABLE `MonkeyTest_plugincontainer` ADD CONSTRAINT plugin_id_refs_id_4efbf983 FOREIGN KEY (`plugin_id`) REFERENCES `MonkeyTest_plugin` (`id`);
COMMIT;

And a few tests of filtering all generate the correct queries with the right table name:

Plugin.objects.filter(id=1) generates:

SELECT `MonkeyTest_plugin`.`id`, `MonkeyTest_plugin`.`name`, `MonkeyTest_plugin`.`description` FROM `MonkeyTest_plugin` WHERE `MonkeyTest_plugin`.`id` = 1

PluginContainer.objects.select_related().filter(id=1) generates:

SELECT `MonkeyTest_plugincontainer`.`id`, `MonkeyTest_plugincontainer`.`title`, `MonkeyTest_plugincontainer`.`plugin_settings`,
 `MonkeyTest_plugincontainer`.`plugin_id`, `MonkeyTest_plugin`.`id`, `MonkeyTest_plugin`.`name`, `MonkeyTest_plugin`.`description`
 FROM `MonkeyTest_plugincontainer` 
INNER JOIN `MonkeyTest_plugin` ON (`MonkeyTest_plugincontainer`.`plugin_id` = `MonkeyTest_plugin`.`id`) 
WHERE `MonkeyTest_plugincontainer`.`id` = 1

If you can demonstrate an action that can actually cause this problem, please post it here, including the SQL statements generated from your database of choice. Otherwise, I am recommending this ticket for closing.

comment:7 Changed 7 years ago by julianb

  • Resolution set to worksforme
  • Status changed from new to closed

One month passed, nothing happened: Closed.

Note: See TracTickets for help on using tickets.
Back to Top