Code

Ticket #10881: models.py

File models.py, 3.2 KB (added by gordyt, 5 years ago)

Sample models to illustrate problem with additional comments at the top

Line 
1"""Testing a problem with db/backends/postgresql/operations.py: sequence_reset_sql
2That method gets called at the end of a loaddata and this part is a problem:
3
4    for f in model._meta.many_to_many:
5        output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
6            (style.SQL_KEYWORD('SELECT'),
7            style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
8            style.SQL_FIELD(qn('id')),
9            style.SQL_FIELD(qn('id')),
10            style.SQL_KEYWORD('IS NOT'),
11            style.SQL_KEYWORD('FROM'),
12            style.SQL_TABLE(qn(f.m2m_db_table()))))
13
14If you have models with a M2M through that doesn't use integer
15PK this code is still executed and that is an error.
16
17The following is self-contained, except that it uses the UUIDField from the
18django_extensions project.
19
20I added 1 Book and 3 Authors and set-up the M2M relationship
21between them.  Then I issued the following commands:
22
23$ python manage.py dumpdata --format=json --indent=4 srtest > srtest_dump.json
24$ python manage.py reset srtest
25$ python manage.py loaddata srtest_dump.json
26
27Here is the output from the loaddata command:
28
29Installing json fixture 'srtest_dump' from absolute path.
30Traceback (most recent call last):
31  File "manage.py", line 11, in <module>
32    execute_manager(settings)
33  File "/usr/local/lib/python2.5/site-packages/django/core/management/__init__.py", line 359, in execute_manager
34    utility.execute()
35  File "/usr/local/lib/python2.5/site-packages/django/core/management/__init__.py", line 304, in execute
36    self.fetch_command(subcommand).run_from_argv(self.argv)
37  File "/usr/local/lib/python2.5/site-packages/django/core/management/base.py", line 195, in run_from_argv
38    self.execute(*args, **options.__dict__)
39  File "/usr/local/lib/python2.5/site-packages/django/core/management/base.py", line 222, in execute
40    output = self.handle(*args, **options)
41  File "/usr/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py", line 196, in handle
42    cursor.execute(line)
43  File "/usr/local/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute
44    return self.cursor.execute(sql, params)
45psycopg2.ProgrammingError: COALESCE types text and integer cannot be matched
46
47This was tested with Django revision 10604
48"""
49from django.contrib import admin
50from django.db import models
51from django_extensions.db.fields import UUIDField
52
53class Author(models.Model):
54    id = UUIDField(primary_key=True)
55    name = models.CharField(max_length=64)
56    def __unicode__(self):
57        return self.name
58
59class Book(models.Model):
60    id = UUIDField(primary_key=True)
61    title = models.CharField(max_length=64)
62    authors = models.ManyToManyField(Author, through='BookAuthors')
63    def __unicode__(self):
64        return self.title
65
66class BookAuthors(models.Model):
67    id = UUIDField(primary_key=True)
68    author = models.ForeignKey(Author)
69    book = models.ForeignKey(Book)
70    def __unicode__(self):
71        return "%s - %s" % (self.author, self.book)
72
73
74class AuthorAdmin(admin.ModelAdmin):
75    pass
76admin.site.register(Author, AuthorAdmin)
77
78class BookAdmin(admin.ModelAdmin):
79    pass
80admin.site.register(Book, BookAdmin)
81
82class BookAuthorsAdmin(admin.ModelAdmin):
83    pass
84admin.site.register(BookAuthors, BookAuthorsAdmin)