﻿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
7856	custom_sql_for_model incorrectly parses the sql file for advanced sql statements	Farhan Ahmad	nobody	"django.core.management.custom_sql_for_model incorrectly parses advanced SQL statements in model sql files.  In my specific case I am adding a [http://www.postgresql.org/docs/8.3/interactive/sql-createrule.html Postgresql RULE] that has sub-statements in it.  In this case the custom sql function breaks up the one rule statement into multiple ones.

I have the following SQL in app/sql/model.sql (replace ''app'' and ''model'' appropriately).
{{{
#!sql
CREATE RULE productlinkimage_onupdate_set_linked AS ON UPDATE TO core_productlinkimage
WHERE NEW.image_id <> OLD.image_id AND NEW.is_deleted = OLD.is_deleted
DO (
	UPDATE core_image SET number_linked = number_linked + 1 WHERE core_image.id = NEW.image_id;
	UPDATE core_image SET number_linked = number_linked - 1 WHERE core_image.id = OLD.image_id
);
}}}

Then when I do `django-admin.py syncdb` it creates the following set of commands to execute separately, and, obviously, fails.

{{{

Starting SQL statement (I have added the line breaks for for better wrapping)
==> u'\nCREATE RULE productlinkimage_onupdate_set_linked AS ON UPDATE TO core_productlinkimage
\nWHERE NEW.image_id <> OLD.image_id AND NEW.is_deleted = OLD.is_deleted\nDO (
\n\tUPDATE core_image SET number_linked = number_linked + 1 WHERE core_image.id = NEW.image_id;',

Next SQL statement
==> u'\tUPDATE core_image SET number_linked = number_linked - 1 WHERE core_image.id = OLD.image_id\n);',
}}}

The exact output is...
{{{
Installing custom SQL for core.Product model
Failed to install custom SQL for core.Product model: syntax error at end of input
LINE 6: ...nked = number_linked + 1 WHERE core_image.id = NEW.image_id;
}}}"		closed	Core (Other)	dev		wontfix		farhan@…	Unreviewed	0	0	0	0	0	0
