Opened 17 years ago

Closed 17 years ago

#4930 closed (duplicate)

foreign key constraint not set in database for FKs to another application's models

Reported by: James <james_027@…> Owned by: Philippe Raoult
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: foreign key backend
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am very new to django, I am creating a model with a field of models.ForeignKey(User) #django.contrib.auth.models import User. When I look at the database my table has no foreign key define. Another example is the django.contrib.admin.models.LogEntry model it has user = models.ForeignKey(User) which is also from django.contrib.auth.models.user.

When the relationship is within the same namespace (am I using the right term, pls correct me) the manage.py syncdb create the tables without a problem if all the tables are create on the same time, but if the tables weren't created the same time it also fail to create the foreign keys.

Is this a bug of django or a shortcomings of database driver?

Thanks
james

Change History (9)

comment:1 by Russell Keith-Magee, 17 years ago

Resolution: worksforme
Status: newclosed

You're going to need to provide an example to prove this doesn't work. Table creation has been used successfully by _many_ people without difficulty.

comment:2 by James <james_027@…>, 17 years ago

Resolution: worksforme
Status: closedreopened

a simple sample is the django.contrib.admin.models.LogEntry from the django.contrib.admin application. where in the django_admin_log table has no foreign key define.

comment:3 by Brian Rosner <brosner@…>, 17 years ago

Resolution: invalid
Status: reopenedclosed

You are not providing enough context and details to prove what you are saying is true. Please move your questions about Django to the django-users mailing list. If you are performing a syncdb after creating a new field in your model then it won't be created since syncdb will only create tables if they don't already exist in your database.

comment:4 by james <james_027@…>, 17 years ago

Resolution: invalid
Status: closedreopened

here is my model,

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
DEPARTMENTS = (

('HRD', 'HRD'),
('ACT', 'Accounting'),
('PUR', 'Purchasing'),
('LOG', 'Logistics'),
('PRO', 'Production'),
('MKG', 'Marketing'),
('EXP', 'Export'),
('ITD', 'IT'),

)

LEVELS = (

('SM', 'Senior Manager'),
('MR', 'Manager'),
('SP', 'Supervisor'),
('ST', 'Staff'),

)

class Profile(models.Model):

"""KSK Employee accounts to use this application"""

user = models.ForeignKey(User)
department = models.CharField(maxlength=3, choices=DEPARTMENTS)
level = models.CharField(maxlength=3, choices=LEVELS)

comment:5 by Chris Beaven, 17 years ago

Resolution: invalid
Status: reopenedclosed

James, as Brian has already said, take this to the django-users mailing group (or IRC at least). Until you have thrashed it out there, leave this ticket closed.

comment:6 by Philippe Raoult, 17 years ago

Keywords: backend added
Resolution: invalid
Status: closedreopened
Summary: foreign key on database not created with models referencing from other applicationforeign key constraint not set in database for FKs to another application's models
Triage Stage: UnreviewedDesign decision needed
Version: 0.96SVN

i think he refers to the same issue as #5113. Basically it seems foreign key are not always marked as such in the DB but are sometimes (how often remains to be seen) created as plain integer fields with no constraints.

comment:7 by Philippe Raoult, 17 years ago

Owner: changed from Adrian Holovaty to Philippe Raoult
Status: reopenednew

#5113 is closed in favor of #4193, which exhibits the same random pattern.

mysite/polls/models.py;

from django.db import models
from mysite.timesheet.models import *

class Choice(models.Model):
    choice = models.CharField(maxlength=200)
    votes = models.IntegerField()
    client = models.ForeignKey(Client)

mysite/timesheet/models.py;

from django.db import models

class Client(models.Model):
    name = models.CharField(maxlength=200)
    shortname = models.CharField('short name', maxlength=5)
    datecreated = models.DateTimeField('date created')
$ ./manage.py sql polls
BEGIN;
CREATE TABLE "polls_choice" (
    "id" integer NOT NULL PRIMARY KEY,
    "choice" varchar(200) NOT NULL,
    "votes" integer NOT NULL,
    "client_id" integer NOT NULL
)
;
COMMIT;

Doing the FK from Client to Choice returning the right SQL. Again it sounds like an ordering bug.

comment:8 by Philippe Raoult, 17 years ago

might be relevant:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'mysite.polls',
    'mysite.timesheet'
)

comment:9 by James Bennett, 17 years ago

Resolution: duplicate
Status: newclosed

Closing as dupe of #4193.

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