0

I am learning the Django framework and I am having trouble trying to create a simple relation between two tables in my model. I am running django and mysql in docker in two separate containers, created by a docker-compose file (code is down below) and so far I have not been able to create the tables (migrations).

When I execute the command:

python manage.py migrate --database=mariadb

I get as a result:

django.db.utils.OperationalError: no such table: Fligts_country

In other posts similar to this one (Django - no such table exception for example) I see that a common solution is to remove the pycache folder and migrations as well, but this is not working for me.

I need the tables to be created in the database, but most importantly I want to know what is exactly wrong with my code. I assume that there is something that I am missing about Foreign keys.

My code so far

Docker-compose.yml

version: '3'

services:

    travel-crawler:
        restart: always
        container_name : travel-crawler
        build: ./Crawler
        tty: true
        ports: 
            - '9005:8080'

        volumes:

          - type: bind
            source : E:\python-code\Traveler\Crawler
            target : /home/shared-files/

    db-traveler:
        image: mariadb
        restart: always
        container_name : db-traveler
        ports: 
            - '3306:3306'

        environment:
            MARIADB_ROOT_PASSWORD: pwd
            MARIADB_DATABASE : travel-crawler
            MARIADB_USER : user
            MARIADB_PASSWORD : password
        #avoid error for STRICT_TRANS_TABLES (error was thrown if no default value was assigned to primary key)
        command: mysqld --sql_mode=""
        #command: python manage.py runserver 0.0.0.0:8000
        expose:
            - '3306:3306'
        volumes:
          # persist data files into `datadir` volume managed by docker
          - dbdatavol:/var/lib/mysql
          # bind-mount any sql files that should be run while initializing
          - ./tables.sql:/docker-entrypoint-initdb.d/schema.sql

volumes:
    dbdatavol:
networks:
    net:
        ipam:
            driver: default
            config:
                - subnet: 212.172.1.0/30
    host:
        name: host
        external: true

Database definition in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'mariadb': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'travel-crawler',
        'USER' : 'user',
        'PASSWORD' : 'password',
        'HOST' : 'db-traveler',
        'PORT' : '3306'
    }
}

app/models.py

from django.db import models
from django.contrib import auth

from django.core.validators import MaxValueValidator, MinValueValidator
# Create your models here.

class Country(models.Model):
    name = models.CharField(help_text = 'Name of the country', max_length = 50)
    code = models.CharField(help_text = 'alpha2_code', max_length = 4)
    code_2 = models.CharField(help_text = 'alpha2_code', max_length = 4)

class ProxyServer(models.Model):
    """
    ProxyServer description for VPN connections
    """

    host = models.CharField(help_text = 'Name of the host', max_length = 30)
    proxy = models.IntegerField(help_text = 'Port to conenct to', validators=[MaxValueValidator(65535), MinValueValidator(1)])
    #country = models.CharField(help_text = 'Country Name')
    
    country = models.ForeignKey(Country, choices=Country.objects.values_list('name', flat=True), on_delete = models.DO_NOTHING)

Edit 1

The problem seems to be the line defining the choices of the foreign key, but I still don't understand why.
choices=Country.objects.values_list('name', flat=True)
M. Villanueva
  • 185
  • 1
  • 17
  • Have you executed **python manage.py makemigrations** before **migrate**? From looking at your code I would say that there is nothing wrong with *choices=Country.objects.values_list('name', flat=True)*!? Try opening your database and check wether the models have been applied. Right now your code tries to make a request to a model that doesn't seem to exist inside your database. – finethen Mar 26 '22 at 15:48
  • Make migrations fails when executed (the error is the same as in the subject) – M. Villanueva Mar 27 '22 at 11:44
  • And what about the table inside the database? Can you take a look and confirm that it exists!? – finethen Mar 28 '22 at 11:54
  • The table does not exist but I understand that this table should be generated automatically when migrations are made – M. Villanueva Mar 28 '22 at 17:25
  • Thats correct! And since django is expecting this table to already exist, it's throwing the error mentioned above. I would suggest to delete all files inside the migrations-folder, but you've already tried that. – finethen Mar 28 '22 at 19:14
  • That's the thing, according to the docs this should work but it is trying to access a table which is not being created. – M. Villanueva Mar 29 '22 at 09:11

0 Answers0