Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda