﻿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
10881	db.backends.postgresql.operations.sequence_reset_sql and M2M fields with non-integer PKs	gordyt	nobody	"Near the end of the command `python manage.py loaddata ...` the `sequence_reset_sql` is called.  This part of the method is run for models that contain many-to-many fields:
{{{
    for f in model._meta.many_to_many:
        output.append(""%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;"" % \
            (style.SQL_KEYWORD('SELECT'),
            style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
            style.SQL_FIELD(qn('id')),
            style.SQL_FIELD(qn('id')),
            style.SQL_KEYWORD('IS NOT'),
            style.SQL_KEYWORD('FROM'),
            style.SQL_TABLE(qn(f.m2m_db_table()))))
}}}

Under normal circumstances this is fine.  But if you are using your own model to manage the M2M relationship (specified via the `through=xxx` parameter in the `ManyToManyField`) and that model uses a non-integer primary key, then this part of the method generates invalid SQL code.

I have attached two files to this ticket.  The `models.py` file has additional comments at the top that shows the output of running the `loaddata` command.  It is self-contained except that is uses the UUIDField from the django-extensions project.  This version of a UUIDField is a simple `CharField` extension.

The file `strtest_dump.json` is a very small file created by the `dumpdata` command.  It has data for 3 authors and 1 book and the entries to join them together.

"		closed	Database layer (models, ORM)	1.0		duplicate	postgresql,operations,sequence_reset_sql,loaddata		Unreviewed	1	0	1	0	0	0
