﻿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
1165	[patch] Wrong column name in (superfluous?) constraint	avandorp@…	Adrian Holovaty	"The following (simplified) model gives the SQL error ""ERROR:  column
""invoice_number"" does not exist"":
{{{
class Invoice(meta.Model):
    invoice_number = meta.PositiveIntegerField(primary_key=True)

class Invoice_item(meta.Model): # General invoice item
    invoice_number = meta.ForeignKey(Invoice)
    id = meta.AutoField('ID', primary_key=True)
}}}
This is to expected because the generated SQL for Invoice_item looks like this:
{{{
CREATE TABLE ""invoicing_invoice_items"" (
    ""invoice_number_id"" integer CHECK (""invoice_number"" >= 0) NOT NULL
REFERENCES ""invoicing_invoices"" (""invoice_number""),
    ""id"" serial NOT NULL PRIMARY KEY
);
}}}
Dody Suria Wijaya on the user mailinglist suggested the following change:

{{{

Index: django/core/management.py
===================================================================
--- django/core/management.py   (revision 1817)
+++ django/core/management.py   (working copy)
@@ -74,7 +74,7 @@
                 data_type = f.get_internal_type()
             col_type = db.DATA_TYPES[data_type]
             if col_type is not None:
-                field_output = [db.db.quote_name(f.column), col_type % rel_field.__dict__]
+                field_output = [db.db.quote_name(f.column), col_type % f.__dict__]
                 field_output.append('%sNULL' % (not f.null and 'NOT ' or ''))
                 if f.unique:
                     field_output.append('UNIQUE')
}}}
I don't know, whether that's all. Where does this additional _id come from? Isn't that only for primary keys? What's that CHECK clause doing there anyway, it's already in the referenced table?

Thanks for your work on Django!

"	defect	closed	Core (Other)	dev	major	fixed			Unreviewed	1	0	0	0	0	0
