gpt4 book ai didi

python - 运行 django 测试会导致重复的列名 id

转载 作者:行者123 更新时间:2023-11-29 05:50:35 24 4
gpt4 key购买 nike

我正在创建一个测试来测试 Django 中的辅助函数。运行测试时,出现错误“列名称 ID 重复”

我尝试运行 python3 manage.py migrate --fake-initial,在 Stackoverflow 上看到了一个解决方案,声称它可以工作,但没有成功。

测试.py:

from django.test import TestCase
from reservations.models import Reservation, Table, Restaurant
from employee.helpers import *


class GetTablesWithCapacityTestCase(TestCase):
def setUp(self):
Restaurant.objects.create(
name="Test Restaurant",
description="Restaurant for use in test",
opening_time=12,
closing_time=24,
)
Table.objects.create(
restaurant=Restaurant.objects.filter(name="Test Restaurant"),
number_of_seats=5,
is_occupied=0
)
Table.objects.create(
restaurant=Restaurant.objects.filter(name="Test Restaurant"),
number_of_seats=4,
is_occupied=0
)

def test_get_tables(self):
tables = get_tables_with_capacity(5)
self.assertEqual(1, tables.size)

模型.py:

from django.db import models
from guest.models import Guest
from django.utils import timezone


class Restaurant(models.Model):
name = models.CharField(max_length=250)
description = models.CharField(max_length=250)
opening_time = models.TimeField()
closing_time = models.TimeField()

def __str__(self):
return self.name


class Table(models.Model):
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, null=True, blank=True)
number_of_seats = models.IntegerField()
is_occupied = models.BooleanField()

def __str__(self):
return str(self.id)


class Reservation(models.Model):
guest = models.ForeignKey(Guest, on_delete=models.CASCADE)
number_of_people = models.IntegerField(default=0)
start_date_time = models.DateTimeField(default=timezone.now)
end_date_time = models.DateTimeField(default=timezone.now)
created_date = models.DateTimeField(default=timezone.now)
table = models.ForeignKey(Table, on_delete=models.CASCADE, null=True, blank=True)

def __str__(self):
return self.start_date_time

编辑 2:访客模型:

class Guest(models.Model):
email = models.EmailField()
reminder = models.BooleanField()

def __str__(self):
return self.email

我在运行测试时得到的结果是:

MySQLdb._exceptions.OperationalError: (1060, "Duplicate column name 'table_id'")

The above exception was the direct cause of the following exception:

django.db.utils.OperationalError: (1060, "Duplicate column name 'table_id'")

编辑 1:完整的堆栈跟踪: What's out of the picture is "Got an error creating the test database: (1007, "Can't create database 'test_stud_pu-29_trippinTacos'; database exists")"

哦,是的,我使用的是 MySQL,而不是内置的 Django SQLite。

我显然希望输出是失败的测试或成功的测试。

希望有人能帮忙!

最佳答案

固定

我删除了所有迁移文件并运行了 python3 manage.py makemigrations。这样就解决了。

关于python - 运行 django 测试会导致重复的列名 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54766832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com