Opened 8 years ago

Closed 7 years ago

Last modified 6 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: UI/UX:

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 Changed 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

This is on the multi-db branch, right?

comment:2 Changed 8 years ago by ben <ben.fordnz@…>

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

comment:3 Changed 8 years ago by SmileyChris

  • Summary changed from bug in relating models across different db's to [multi-db] bug in relating models across different db's

comment:4 Changed 8 years ago by anonymous

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 Changed 8 years ago by Simon G. <dev@…>

  • Has patch set
  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 8 years ago by Ben

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 Changed 7 years ago by SmileyChris

  • Version changed from SVN to other branch

comment:8 Changed 7 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

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 Changed 6 years ago by ehsan@…

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