배포를 위해 sqlite를 mysql(mariadb)로 바꾸고 migrate했을 때 외래키 관련 오류가 발생합니다..
wyw/models.py
from django.db import models
from account.models import User
from django.urls import reverse
from account.models import Profile
class Category(models.Model):
name = models.CharField(max_length=20, unique=True)
description = models.CharField(max_length=200, null=True, blank=True)
has_comment = models.BooleanField(default=True) # 답변가능 여부
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('wyw:index', args=[self.name])
class Posting(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author_question')
author_avatar = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='author_question')
modify_date = models.DateTimeField(null=True, blank=True)
subject = models.CharField(max_length=200)
content = models.TextField()
create_date = models.DateTimeField()
voter = models.ManyToManyField(User, related_name='voter_question') # 추천인 추가
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='category_posting')
scraper = models.ManyToManyField(User, related_name='scrap_posting') # through는 연결할 모델을 의미한다.
scrap_counter = models.IntegerField(default=0) # 불필요한 연산 없이 진행하기 위해.
user_Rating = models.FloatField(default=0)
def __str___(self):
return self.subject
class Comment(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
modify_date = models.DateTimeField(null=True, blank=True)
Posting = models.ForeignKey(Posting, on_delete=models.CASCADE)
score = models.IntegerField(default=0)
content = models.TextField()
create_date = models.DateTimeField()
account/models.py
from django.db import models
from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)
from PIL import Image
favorites_list = (
('백엔드', '백엔드'),
('프론트엔드', '프론트엔드'),
('Andriod','Andriod'),
('iOS','iOS')
)
class UserManager(BaseUserManager):
def create_user(self, email, name,date_of_birth, favorites_1, favorites_2 , image, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
date_of_birth=date_of_birth,
favorites_1 = favorites_1,
favorites_2 = favorites_2,
name = name,
image = image,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, favorites_1, favorites_2, image, password):
user = self.create_user(
email,
password=password,
date_of_birth=date_of_birth,
favorites_1 =favorites_1,
favorites_2 = favorites_2,
image = image,
)
user.is_admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
favorites_1 = models.CharField(max_length= 200, choices = favorites_list, default='')
favorites_2 = models.CharField(max_length= 200, choices = favorites_list, default='')
name = models.CharField(max_length= 200, default='')
image = models.ImageField(upload_to='media/')
followings = models.ManyToManyField('self', symmetrical=False, related_name='followers')
def __str__(self):
return self.user.email
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth','favorites_1','favorites_2','name','image']
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
# class Profile(models.Model):
# user = models.OneToOneField(User, on_delete=models.CASCADE)
# image = models.ImageField(blank=True)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(default='media/button.png', upload_to='media/')
def __str__(self):
return self.user.email
# resizing images
def save(self, *args, **kwargs):
super().save()
img = Image.open(self.avatar.path)
new_img = (132, 132)
img.thumbnail(new_img)
img.save(self.avatar.path)
코드는 이렇고 mysqlclient 패키지를 설치한 뒤에 makemigration 하고 migrate하면 django.db.utils.OperationalError: (1005, 'Can\'t create table mysql
.#sql-748_30
(errno: 150 "Foreign key constraint is incorrectly formed")')
이런 오류메시지가 뜨면서 migrate가 안됩니다...
외래키 오류라고 떠서 foreignkey 관련된 문항들을 OneToOne으로 바꿔보기도 했지만 여전히 외래키 오류가 뜨더라고요... 무슨 문제일까요??
lgj 님 440
2022년 8월 17일 9:45 오전