﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
1928	"[patch] Forget some foreignkeys during ""python manage.py syncdb"" with an empty postgres db."	hornero	Adrian Holovaty	"For models with several foreignkeys to the same table, like this :

{{{
class File(models.Model):
    Name = models.CharField(maxlength=200,null=False, blank=False, unique=True)
    Storage = models.CharField(maxlength=200, null=True, blank=True)
    def __repr__(self):
        return self.Name
}}}

{{{
class Job(models.Model):
    Name = models.CharField(maxlength=200)
    Script_FK = models.ForeignKey(File,related_name='Script', verbose_name=""The script file"",null=True, blank=True)
    JobLog_FK = models.ForeignKey(File,related_name='JobLog', verbose_name=""The log file"",null=True, blank=True)
    def __repr__(self):
        return self.Name
}}}

{{{
class Header(models.Model):
    FULL_PATH_FITS = models.CharField(maxlength=200, null=False, blank=False, unique=True)
    Fits_File_FK = models.ForeignKey(File, related_name='Fits_File_FK', verbose_name=""The related File"",null=True, blank=True)       
    def __repr__(self):
        return str(int(self.FULL_PATH_FITS))
}}}

When we launch python manage.py syncdb"" with an empty postgres db, some foreignkeys are missing :

{{{

=> \d process_job
                                    Table ""public.process_job""
    Column    |          Type          |                        Modifiers
--------------+------------------------+----------------------------------------------------------
 id           | integer                | not null default nextval('process_job_id_seq'::regclass)
 Name         | character varying(200) | not null
 Script_FK_id | integer                |
 JobLog_FK_id | integer                |
Indexes:
    ""process_job_pkey"" PRIMARY KEY, btree (id)
Foreign-key constraints:
    ""JobLog_FK_id_referencing_process_file_id"" FOREIGN KEY (""JobLog_FK_id"") REFERENCES process_file(id)
    ""Script_FK_id_referencing_process_file_id"" FOREIGN KEY (""Script_FK_id"") REFERENCES process_file(id)

=> \d process_header
                                       Table ""public.process_header""
       Column       |          Type          |                          Modifiers
--------------------+------------------------+-------------------------------------------------------------
 id                 | integer                | not null default nextval('process_header_id_seq'::regclass)
 FULL_PATH_FITS     | character varying(200) | not null
 Fits_File_FK_id    | integer                |
Indexes:
    ""process_header_pkey"" PRIMARY KEY, btree (id)
    ""process_header_FULL_PATH_FITS_key"" UNIQUE, btree (""FULL_PATH_FITS"")
}}}

I think the problem start with the update of the dictionnary ""pending_references"" in django/core/management.py :
(In the function syncdb()):

{{{
449 	            sql, references = _get_sql_model_create(model, seen_models)
450 	            seen_models.add(model)
451 	            created_models.add(model)
452 	            pending_references.update(references)        #===============> Problem...
453 	            sql.extend(_get_sql_for_pending_references(model, pending_references))
}}}


The update function overwrites the previous references to the file table with the new ones.

In order to keep the previous foreignkeys, I've written the folowing function to replace the update dictionnary function :

{{{
def update_dict(dict_input = {},dict_update = {}):
    ""New update function which keep the previous references.""     
     dict_output = dict_input     
     try :
     	for k_update, v_update in dict_update.items() :
     		try :
			value_to_update = dict_input[k_update]
			value_output = value_to_update + v_update
			dict_output[k_update] = value_output	
		except :
			dict_output[k_update] = v_update
     except :
     	dict_output.update(dict_update)
     return dict_output
}}}

and call it in syncdb():

{{{
pending_references = update_dict(pending_references,references)
}}}


The same error can be found in get_sql_create(app).

Now, in the db, we have :


{{{
\d process_header                                        Table ""public.process_header""        Column       |          Type          |                          Modifiers
--------------------+------------------------+-------------------------------------------------------------
 id                 | integer                | not null default nextval('process_header_id_seq'::regclass)
 FULL_PATH_FITS     | character varying(200) | not null
 Fits_File_FK_id    | integer                |
Indexes:
    ""process_header_pkey"" PRIMARY KEY, btree (id)
    ""process_header_FULL_PATH_FITS_key"" UNIQUE, btree (""FULL_PATH_FITS"")
Foreign-key constraints:
    ""Fits_File_FK_id_referencing_process_file_id"" FOREIGN KEY (""Fits_File_FK_id"") REFERENCES process_file(id)

\d process_job                                     Table ""public.process_job""     Column    |          Type          |                        Modifiers
--------------+------------------------+----------------------------------------------------------
 id           | integer                | not null default nextval('process_job_id_seq'::regclass)
 Name         | character varying(200) | not null
 Script_FK_id | integer                |
 JobLog_FK_id | integer                |
Indexes:
    ""process_job_pkey"" PRIMARY KEY, btree (id)
Foreign-key constraints:
    ""JobLog_FK_id_referencing_process_file_id"" FOREIGN KEY (""JobLog_FK_id"") REFERENCES process_file(id)
    ""Script_FK_id_referencing_process_file_id"" FOREIGN KEY (""Script_FK_id"") REFERENCES process_file(id)

}}}
"	defect	closed	Core (Management commands)	dev	major	fixed	Forget foreignkey django-admin.py	landonf@…	Unreviewed	1	0	0	0	0	0
