it only happens when I create new chatroom with the same admin this is what I wrote in my models.py
class ChatRoom(models.Model):
id = models.UUIDField(primary_key=True, unique=True,
default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100, null=False, blank=True)
users = models.ManyToManyField(User, through='Membership')
admin = models.ForeignKey(
User, null=False, blank=False, on_delete=models.CASCADE, related_name='admin')
date_created = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Membership(models.Model):
id = models.UUIDField(primary_key=True, unique=True,
default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
chatroom = models.ForeignKey(ChatRoom, on_delete=models.CASCADE)
date_joined = models.DateTimeField(auto_now=True, null=False, blank=False)
def __str__(self):
return self.user
class Meta:
unique_together = [['user', 'chatroom']]
when i write this in the shell:
from .main.models import ChatRoom,Membership
from django.contrib.auth.models import User
user = User.objects.get(username = 'someone')
chatroom = ChatRoom(admin = user, name = 'something')
chatroom.save()
chatroom2 = ChatRoom(admin = user, name = 'somethingElse')
chatroom2.save()
after i save chatroom2 i get this error: django.db.utils.IntegrityError: UNIQUE constraint failed: main_chatroom.admin_id
can anyone help me?
so it turned out that i did something that made all migrations does not have any affect on the database so i created new project and copied all my code to the new project( yes i know this is not the right way to do things but it was the easiest way for me) and everything now works great
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.