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)