﻿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
14019	SQLInsertCompiler.as_sql() failure	Mark Lavin	Tobias McNulty	"I came across a problem with `SQLInsertCompiler.as_sql` function while trying to get a stacktrace printed on insert errors (using [http://github.com/dcramer/django-db-log django-db-log]). I found that the `as_sql` function has an implicit condition that `execute_sql` must be called first because `execute_sql` sets the `return_id` attribute. This simple sequence shows the issue using any core db-backend:

{{{
from django.contrib.auth.models import User
from django.db import router
from django.db.models.sql import InsertQuery

db = router.db_for_write(User)
query = InsertQuery(User)
query.insert_values([(User._meta.fields[0], 1)], raw_values=False)
print query.get_compiler(using=db).as_sql()
}}}

yields

{{{
AttributeError: 'SQLInsertCompiler' object has no attribute 'return_id'
}}}

I've attached a patch which checks for the existence of the `return_id` attribute and defaults to `False` if not found (as is the convention in `execute_sql`).

"	Bug	closed	Database layer (models, ORM)	1.2	Normal	fixed		Michael Manfre	Design decision needed	1	0	0	0	0	0
