﻿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
7516	M2M.get_or_create() not mapping correctly	petersanchez@…	nobody	"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.

{{{
#!python
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.

"		closed	Database layer (models, ORM)	dev		fixed			Accepted	0	0	0	0	0	0
