@kicherov_maxim

Почему не накатывается миграция Django на MySql?

Добрый вечер. В общем когда делаю у себя локально ./manage.py migrate на sqllite все работает, а на MySql в продакшене ошибка.
Текст ошибки
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See
: https://docs.djangoproject.com/en/3.0/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, fingerprinter, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying fingerprinter.0001_initial... OK
  Applying fingerprinter.0002_auto_20200302_0728...Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 565, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 678, in _alter_field
    old_default = self.effective_default(old_field)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 303, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 282, in _effective_default
    default = field.get_default()
  File "/home/kicherov/fingerprint/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 829, in get_default
    return self._get_default()
TypeError: calculate_tlsh() missing 1 required positional argument: 'self'


from django.db import models
import tlsh
from django.contrib import admin
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.auth.models import User
# Create your models here.


class Font(models.Model):
    name = models.CharField(max_length=255, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["name"]


class Language(models.Model):
    name = models.CharField(max_length=50, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["name"]


class FingerPrint(models.Model):

    def result_string(self):
        return self.user_agent + str(self.time_zone) + str(self.screen_h) + str(self.screen_w) + \
               str(self.hardware_concurrency) + ''.join(lng.name for lng in self.browser_languages.all()) + \
               self.unmasked_vendor_webgl + self.unmasked_renderer_webgl + self.canvas_fingerprint3d + \
               self.canvas_fingerprint + '1'.join(font.name for font in self.installed_fonts.all()) + \
               '0'.join(font.name for font in self.not_installed_fonts.all())

    def calculate_tlsh(self):
        return tlsh.forcehash(self.result_string().encode())

    user = models.OneToOneField(User, blank=True, on_delete=models.CASCADE, null=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    ip = models.CharField(max_length=50, blank=True)
    user_agent = models.CharField(max_length=255, blank=True)
    time_zone = models.IntegerField(blank=True)
    screen_w = models.IntegerField(blank=True)
    screen_h = models.IntegerField(blank=True)
    hardware_concurrency = models.IntegerField(blank=True)
    browser_languages = models.ManyToManyField('Language', related_name='browser_languages')
    unmasked_vendor_webgl = models.CharField(max_length=255, blank=True)
    unmasked_renderer_webgl = models.CharField(max_length=255, blank=True)
    canvas_fingerprint = models.CharField(max_length=255, blank=True)
    canvas_fingerprint3d = models.CharField(max_length=255, blank=True)
    installed_fonts = models.ManyToManyField(Font, related_name='installed_fonts')
    not_installed_fonts = models.ManyToManyField('Font', related_name='not_installed_fonts')
    tlsh = models.CharField(max_length=400, blank=True)

    # def save(self, *args, **kwargs):
    #     #super(FingerPrint, self).save(*args, **kwargs)
    #     self.tlsh = self.calculate_tlsh()
    #     print(':', self.tlsh)
    #     super(FingerPrint, self).save(*args, **kwargs)


@receiver(post_save, sender=FingerPrint)
def update_calculated_fields(sender, instance, **kwargs):
    tlsh = instance.calculate_tlsh()
    sender.objects.filter(pk=instance.pk).update(tlsh=tlsh)
  • Вопрос задан
  • 80 просмотров
Решения вопроса 1
sergey-gornostaev
@sergey-gornostaev Куратор тега Django
Седой и строгий
Проблема не в приведённом вами коде. Вероятно, она в предыдущей миграции. Судя по трейсбэку, возникает она при попытке получить значение по умолчанию для поля. Возможно, у вас была попытка вызова метода из дескриптора, что по определению работать не может.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

Войти через центр авторизации
Похожие вопросы