mardi 19 février 2019

Running django tests results in duplicate column name id

I'm creating a test for testing a helper function in Django. When running the test, I get the error "Duplicate column name ID"

I've tried to run python3 manage.py migrate --fake-initial, saw a solution here on Stackoverflow stating that it would work, but it didn't.

Test.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)

Models.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

The result I get when running the test is:

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'")

Oh yeah, I'm using MySQL, not the built-in Django SQLite.

I obviously wanted the output to be either a failed test or a successful test.

Hope someone can help!

Aucun commentaire:

Enregistrer un commentaire