I have inherited a large Django application and it features this idiom in the model for the “shows” application:
class Movie_Playlist(models.Model):
name = models.CharField(max_length=150)
# More fields here...
players = models.ManyToManyField(
User, related_name='playlists_played', null=True, blank=True)
class PlaylistPlayer(models.Model):
user = models.ForeignKey(User, related_name='play_links')
movie_playlist = models.ForeignKey(Movie_Playlist, related_name='playlist_plays')
count = models.IntegerField(default=0)
last_played = models.DateTimeField(editable=False)
class Meta:
db_table = 'shows_movie_playlist_players'
As near as I can tell, the idea may be that the shows_movie_playlist_players table (which would be created by the ManyToManyField relation) is supposed to do double duty by storing per-user information in addition to just the relationship itself. Certainly, there seem to only be two related tables in the original database: “shows_movie_playlist” and “shows_movie_playlist_players”.
Also, for what it’s worth, this “validates” fine in Django 1.2.3 — 1.3.1.
This would all be fine and dandy, except that it breaks the “python manage.py test” method when it tries to create the test database since the shows_movie_playlist_players has already been created by the “ManyToManyField…” specification by the time it gets to the PlaylistPlayer class.
If I comment out the “ManyToManyField…” line, I can create the test database and run tests with no problem. Otherwise, I get this:
> python manage test shows -v3
Creating test database for alias 'default' ('test_production')...
Creating tables ...
Creating table shows_movie_playlist_players
Creating table shows_movie_playlist
Creating table shows_movie_playlist_players
Traceback (most recent call last):
File "manage.py", line 16, in <module>
execute_manager(settings)
... Lots of tracebackage here...
_mysql_exceptions.OperationalError: (1050, "Table 'shows_movie_playlist_players' already exists")
So is this a properly specified relation and overridden table that just doesn’t have support for testing yet or is something horribly wrong?
Thanks for reading!
Oh wow… I feel for you 🙂 Yes, it’s horribly wrong but it might have a simple solution.
Basically it’s a hack which tries to accomplish the “through” functionality of a
ManyToManyFieldTry specifying
through='shows.PlaylistPlayer' on theplayers` m2m field and see how it behaves then.