1

I have tested to "inspectdb" but I think my schema is to complex, for example... for the table "tdir_files_context" (djangoinspectdb.JPG "image in attachment") I got this model:

class TdirFilesContext(models.Model):
   id_category = models.ForeignKey(TdirCategories, db_column='id_category')
   id_file_context = models.CharField(max_length=20)
   n_file_context = models.CharField(max_length=150)
   coment = models.CharField(max_length=2000)
   id_user_db_ins = models.CharField(max_length=45)
   id_user_db_upd = models.CharField(max_length=45)
   id_user_sys_ins = models.CharField(max_length=45)
   id_user_sys_upd = models.CharField(max_length=45)
   date_ins = models.DateTimeField()
   date_last_upd = models.DateTimeField()
   class Meta:
       db_table = u'tdir_files_context'

This database table have Two primary keys and One foreign key. The Django Model handle this kind of database tables?

enter image description here

André
  • 24,706
  • 43
  • 121
  • 178

2 Answers2

4

First, no table can have two primary keys. That's just not possible. I'll just assume yours has a primary key composed of two columns, id_category and id_file_context.

Unfortunately, Django doesn't currently support models with composite primary keys, it can only handle tables with exactly one columns as their primary key.

<plug type="shameless">There is work underway on making this possible with Django, see https://code.djangoproject.com/ticket/373 and my work on this from last Google Summer of Code: https://github.com/koniiiik/django </plug>

Other than that though, ForeignKeys are perfectly fine with Django and I see no other problems with your table.

koniiiik
  • 4,240
  • 1
  • 23
  • 28
  • Thanks for your reply. I will try your code. I see this is a long time waiting feature in Django... – André Nov 04 '11 at 13:14
2

koniiiik just brought a very interesting answer about composite primary keys.

But when I see your definition of the model, I'm not sure you really want two primary keys, but just one foreign key and one primary key as :

class TdirCategories(models.Model):
   id_category = models.AutoField(primary_key=True)

class TdirFilesContext(models.Model):
   id_category = models.ForeignKey(TdirCategories, db_column='id_category')
   id_file_context = models.AutoField(primary_key=True)
   n_file_context = models.CharField(max_length=150)
   coment = models.CharField(max_length=2000)
   id_user_db_ins = models.CharField(max_length=45)
   id_user_db_upd = models.CharField(max_length=45)
   id_user_sys_ins = models.CharField(max_length=45)
   id_user_sys_upd = models.CharField(max_length=45)
   date_ins = models.DateTimeField()
   date_last_upd = models.DateTimeField()
   class Meta:
       db_table = u'tdir_files_context'

the command python manage.py syncdb executes it well and after a python manage.py name_of_your_app_containing_the_model sql

You get a correct :

BEGIN;
CREATE TABLE "app_tdircategories" (
    "id_category" integer NOT NULL PRIMARY KEY
)
;
CREATE TABLE "tdir_files_context" (
    "id_category" integer NOT NULL REFERENCES "app_tdircategories" ("id_category"),
    "id_file_context" integer NOT NULL PRIMARY KEY,
    "n_file_context" varchar(150) NOT NULL,
    "coment" varchar(2000) NOT NULL,
    "id_user_db_ins" varchar(45) NOT NULL,
    "id_user_db_upd" varchar(45) NOT NULL,
    "id_user_sys_ins" varchar(45) NOT NULL,
    "id_user_sys_upd" varchar(45) NOT NULL,
    "date_ins" datetime NOT NULL,
    "date_last_upd" datetime NOT NULL
)
;
COMMIT;

hope it helps :)

Guillaume Cisco
  • 2,859
  • 24
  • 25
  • Thanks for the reply. The database schema is correct. This schema is working in production servers for a couple of years and I can't change them. – André Nov 04 '11 at 13:15