@ilyaa01
Новичёк не судите строго

Django ошибка при миграции M2M fields. В чём проблема?

django

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator


class Song(models.Model):
    KEYS = [
        ('Ab', 'Ab'),
        ('A', 'A'),
        ('A#', 'A#'),
        ('Bb', 'Bb'),
        ('H', 'H'),
        ('C', 'C'),
        ('C#', 'C#'),
        ('Db', 'Db'),
        ('D', 'D'),
        ('D#', 'D#'),
        ('Eb', 'Eb'),
        ('E', 'E'),
        ('F', 'F'),
        ('F#', 'F#'),
        ('Gb', 'Gb'),
        ('G', 'G'),
        ('G#', 'G#'),
    ]
    
    name = models.CharField(help_text="Название песни", max_length=250, null=True)
    media = models.URLField(help_text="Ссылка на видео с YouTube", null=True, blank=True)
    key = models.CharField(help_text="Тональность", max_length=2, choices=KEYS, default="C", null=True, blank=True)
    bpm = models.PositiveIntegerField(help_text="Метроном", validators=[MaxValueValidator(400)], null=True, blank=True)
    tse = models.CharField(help_text="Размер Такта", max_length=5, null=True, blank=True)
    text = models.TextField(help_text="Текст песни", max_length=10000, null=True)
    structure = models.TextField(help_text="Структура", max_length=250, null=True, blank=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True, null=True)
    updated = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        permissions = [
            ("can_edit", "Can edit Song"),
        ]

    def __str__(self):
        return self.name

class TypeMinistry(models.Model):
    name = models.CharField(help_text="Название", max_length=250, null=True)

class SlideMinistry(models.Model):
    name = models.CharField(help_text="Название", max_length=250, null=True)
    description = models.TextField(help_text="Описание", null=True, blank=True)

class SongMinistry(models.Model):
    name = models.CharField(help_text="Название", max_length=250, null=True, blank=True)
    songs = models.ManyToManyField(Song, help_text="Песни", blank=True)

class Ministry(models.Model):
    date = models.DateField(help_text="Дата планирования", null=True)
    time = models.TimeField(help_text="Время планирования", null=True)
    name = models.CharField(help_text="Название", max_length=250, null=True)

    type = models.ForeignKey(TypeMinistry, on_delete=models.CASCADE, help_text="Вид служения", related_name="types", null=True, blank=True)
    description = models.TextField(help_text="Описание", null=True, blank=True)
    bible_passage = models.TextField(help_text="Места из Библии", null=True, blank=True)
    slides = models.ManyToManyField(SlideMinistry, help_text="Слайды", blank=True)
    songs = models.ManyToManyField(Song, help_text="Программа", related_name="new_songs", blank=True)
    invited_user = models.ManyToManyField(User, help_text="Пригласить пользователя", blank=True)

    created = models.DateTimeField(auto_now_add=True, null=True)
    updated = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        db_table = "ministry"
        permissions = [
            ("can_edit", "Can edit Ministry"),
        ]

    def __str__(self):
        return self.name


ValueError

python manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, index, sessions
Running migrations:
  Applying index.0008_alter_ministrys_songs...Traceback (most recent call last):
  File "D:\Ilya\Programming\django\PRTC\manage.py", line 22, in <module>
    main()
  File "D:\Ilya\Programming\django\PRTC\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\commands\migrate.py", line 356, in handle      
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\executor.py", line 167, in _migrate_all_forwards 
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\executor.py", line 252, in apply_migration       
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\migration.py", line 132, in apply
    operation.database_forwards(
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\migrations\operations\fields.py", line 235, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Users\Ilya\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\base\schema.py", line 862, in alter_field
    raise ValueError(
ValueError: Cannot alter field index.Ministrys.songs into index.Ministrys.songs - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)

  • Вопрос задан
  • 170 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Everything_is_bad
ValueError: Cannot alter field index.Ministrys.songs into index.Ministrys.songs - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
перевести за тебя?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы