Opened 18 years ago

Closed 16 years ago

Last modified 15 years ago

#3885 closed (wontfix)

[multi-db] bug in relating models across different db's

Reported by: ben Owned by: nobody
Component: Core (Other) Version: other branch
Severity: Keywords: multiple db
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've found what i believe to be a bug when trying to relate models to others in different databases. What seems to be happening is that the wrong opts is being used to build the where clause... So using the following models and query:

Models:
Ticket: postgresql db
WO: mysql db with the following field:

ticket = models.ForeignKey(Ticket, db_column='NOCTicket',blank=True, related_name='workorders',maxlength=60)

Query:
t = Ticket.objects.get(pk=2)
qs = t.workorders.all()

I ended up with the following from qs._get_sql_clause():
(['workorders.WorkorderID',

'workorders.WorkorderNo',
'workorders.RequestorNo',
'workorders.ProblemDescription',
'workorders.DateReceived',
'workorders.ActDateStart',
'workorders.ActDateEnd',
'workorders.DateRequired',
'workorders.EstDuration',
'workorders.ActionTaken',
'workorders.WorkType',
'workorders.CauseDescription',
'workorders.PreventionTaken',
'workorders.WorkStatus',
'workorders.Workpriority',
'workorders.AcceptedBy',
'workorders.SiteCode',
'workorders.NOCTicket',
'workorders.NOC',
'workorders.AlarmCode'],

' FROM workorders WHERE ("workorders"."NOCTicket" = %s)',
[2])

So the select is correct mysql syntax but the where is portgresql syntax.

I'm digging through db.models.query at the moment which I think is where the problem lies, but I'm not all that familiar with this part of the django code. Just to mention as well, I have code from several different branches mixed together too!
Thanks Ben

Change History (9)

comment:1 by Malcolm Tredinnick, 18 years ago

This is on the multi-db branch, right?

comment:2 by ben <ben.fordnz@…>, 18 years ago

Correct, although as I said I have code from a couple of different branches fixed together in my setup

comment:3 by Chris Beaven, 18 years ago

Summary: bug in relating models across different db's[multi-db] bug in relating models across different db's

comment:4 by anonymous, 18 years ago

Ok I believe I've found the source of this problem, in django.db.models.query around line 850. In the part of lookup_inner that checks for a one-to-one or many-to-one field, new_opts is set to field.rel.to._meta... I belive this should be set back to opts:

From:

if field:
            if field.rel: # One-to-One/Many-to-one field
                new_table = current_table + '__' + name
                new_opts = field.rel.to._meta
                new_column = new_opts.pk.column
                join_column = field.column
                raise FieldFound

To:

if field:
            if field.rel: # One-to-One/Many-to-one field
                new_table = current_table + '__' + name
                new_opts = opts
                new_column = new_opts.pk.column
                join_column = field.column
                raise FieldFound

As it's such a simple change I haven't supplied a patch.
Ben

comment:5 by Simon G. <dev@…>, 18 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

comment:6 by Ben, 17 years ago

Unfortunately my quick assumption above has made in haste... making that change breaks lookups spanning related objects on the same database using django's db-api syntax. I will try and have a deeper look into this preoblem and supply a proper patch when I have the chance.

comment:7 by Chris Beaven, 17 years ago

Version: SVNother branch

comment:8 by Malcolm Tredinnick, 16 years ago

Resolution: wontfix
Status: newclosed

Since multidb is unsupported as a branch, it's not really worth having these tickets open. They're never going to be fixed.

comment:9 by ehsan@…, 15 years ago

I have two models which are present in different Database i have ForeignKey in one model which access the other models which is present in another database .How i can do this in Django Can anyone Please help

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