Opened 16 years ago

Last modified 13 years ago

#7516 closed

M2M.get_or_create() not mapping correctly — at Version 1

Reported by: petersanchez@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

Current Setup: Django SVN (rev: 7534), PostgreSQL 8.2.7, Python 2.5.2 (r252:60911, Apr 16 2008, 02:16:20)

Seems like m2m.get_or_create is not actually creating entries in the actual "mapping table" in the database... See below.

from chat.models import *
from django.db import connection
c = Campaign.objects.all()[0]
c.reports.all()
[]
import datetime
c.reports.create(type=REPORT_DEFAULT, date=datetime.date.today())
<Report: <Default Report 21>>
c.reports.all()
[<Report: <Default Report 21>>]
c.reports.get_or_create(type=REPORT_DEFAULT, date=datetime.date.today())
(<Report: <Default Report 21>>, False)
a = Agent.objects.all()[0]
c.reports.get_or_create(type=REPORT_AGENT, agent=a, date=datetime.date.today())
(<Report: <Agent Report 22>>, True)
c.reports.all()
[<Report: <Default Report 21>>]
c.reports.get_or_create(type=REPORT_AGENT, agent=a, date=datetime.date.today())
(<Report: <Agent Report 23>>, True)
c.reports.all()
[<Report: <Default Report 21>>]

import pprint
pprint.pprint(connection.queries)
[{'sql': 'SELECT "chat_campaign"."id", "chat_campaign"."user_id", "chat_campaign"."name", "chat_campaign"."chat_title", "chat_campaign"."is_active", "chat_campaign"."date", "chat_campaign"."updated" FROM "chat_campaign" ORDER BY "chat_campaign"."id" ASC LIMIT 1',
  'time': '0.003'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1 ',
  'time': '0.002'},
 {'sql': 'INSERT INTO "chat_report" ("type", "agent_id", "browser", "date", "attempts", "clicks", "interactions", "sales") VALUES (1, NULL, 0, \'2008-06-20\', 1, 0, 0, 0)',
  'time': '0.001'},
 {'sql': 'SELECT CURRVAL(\'"chat_report_id_seq"\')', 'time': '0.001'},
 {'sql': 'SELECT "report_id" FROM "chat_campaign_reports" WHERE "campaign_id" = 1 AND "report_id" IN (21)',
  'time': '0.001'},
 {'sql': 'INSERT INTO "chat_campaign_reports" ("campaign_id", "report_id") VALUES (1, 21)',
  'time': '0.000'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1 ',
  'time': '0.001'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1  AND "chat_report"."date" = \'2008-06-20\'  AND "chat_report"."type" = 1 ',
  'time': '0.001'},
 {'sql': 'SELECT "chat_agent"."id", "chat_agent"."user_id", "chat_agent"."campaign_id", "chat_agent"."name", "chat_agent"."photo_id", "chat_agent"."date", "chat_agent"."updated", "chat_agent"."is_active" FROM "chat_agent" ORDER BY "chat_agent"."name" ASC LIMIT 1',
  'time': '0.002'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1  AND "chat_report"."date" = \'2008-06-20\'  AND "chat_report"."type" = 2  AND "chat_report"."agent_id" = 2 ',
  'time': '0.001'},
 {'sql': 'INSERT INTO "chat_report" ("type", "agent_id", "browser", "date", "attempts", "clicks", "interactions", "sales") VALUES (2, 2, 0, \'2008-06-20\', 1, 0, 0, 0)',
  'time': '0.001'},
 {'sql': 'SELECT CURRVAL(\'"chat_report_id_seq"\')', 'time': '0.000'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1 ',
  'time': '0.001'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1  AND "chat_report"."date" = \'2008-06-20\'  AND "chat_report"."type" = 2  AND "chat_report"."agent_id" = 2 ',
  'time': '0.001'},
 {'sql': 'INSERT INTO "chat_report" ("type", "agent_id", "browser", "date", "attempts", "clicks", "interactions", "sales") VALUES (2, 2, 0, \'2008-06-20\', 1, 0, 0, 0)',
  'time': '0.001'},
 {'sql': 'SELECT CURRVAL(\'"chat_report_id_seq"\')', 'time': '0.000'},
 {'sql': 'SELECT "chat_report"."id", "chat_report"."type", "chat_report"."agent_id", "chat_report"."browser", "chat_report"."date", "chat_report"."attempts", "chat_report"."clicks", "chat_report"."interactions", "chat_report"."sales" FROM "chat_report" INNER JOIN "chat_campaign_reports" ON ("chat_report"."id" = "chat_campaign_reports"."report_id") WHERE "chat_campaign_reports"."campaign_id" = 1 ',
  'time': '0.001'}]

I can verify via "select * from chat_campaign_reports;" that the mapping is not being inserted.

Change History (1)

comment:1 by Ramiro Morales, 16 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top