Pradeep Saini
Pradeep Saini

Reputation: 336

Unexpected Circular Dependency in migration files

Error - django.db.migrations.exceptions.CircularDependencyError: accounts.0001_initial, songs.0001_initial

I have two apps accounts and songs. Songs have two model files - models.py and song_metadata_models.py

accounts/models.py

class AppUser(models.Model):

  user = models.OneToOneField(User)

  user_languages = models.ManyToManyField('songs.SongLang')

  user_genres = models.ManyToManyField('songs.SongGenre')

  def __str__(self):
      return self.user.username

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

songs/song_metadata_models.py

class SongGenre(models.Model):

  short_name = models.CharField(max_length=10)

  full_name =  models.CharField(max_length=100)

  def __str__(self):
      return self.full_name

class SongLang(models.Model):    


  short_name = models.CharField(max_length=10)

  full_name =  models.CharField(max_length=100)

  def __str__(self):
      return self.full_name

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

songs/models.py

class Song(models.Model):

# Fields
  name = CharField(max_length=255)
  slug = AutoSlugField(populate_from='name', blank=True)
  created = DateTimeField(auto_now_add=True, editable=False)
  last_updated = DateTimeField(auto_now=True, editable=False)
  url = CharField(max_length=100)
  artist = CharField(max_length=50)
  album = CharField(max_length=50)
  like = BooleanField(default=False)
  dislike = BooleanField(default=False)

  # Relationship Fields
  requested_by = ForeignKey('accounts.AppUser', related_name='song_requested_by')
  dedicated_to = ManyToManyField('accounts.AppUser', null = True, blank = True,related_name='song_dedicated_to')
  recommended_to = ManyToManyField('accounts.AppUser', null = True, blank = True,related_name='song_recommended_to')

How to solve this? There is no circular dependency at models level then why this issue?

Upvotes: 0

Views: 228

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

Solution 1: move the AppUser's many2many to SongLang and SongGenre. A m2m relationship is by default symetric so you can declare it on either side of the relationship (nb: just make sure to set the related_name to how your fields was named in the AppUser model).

Solution 2: move your SongGenre and SongLang models to a third app (the canonical solution to circular dependencies)

Solution 3: eventually try to first create the AppUser model without the m2m fields, creates the initial migration, then add the m2m fields (not sure it will work though).

Upvotes: 1

Related Questions