How to create model's tables in MySQL?
I'm trying to replicate a PHP/Symfony project in Python/Django as a python learning exercise. Platform = Windows 10. The expected result is that a migrate
command will add tables related to all of the entries in settings.py INSTALLED_APPS{...}
. Instead, the migrate
command adds all Django
tables but none of the tables of models.py
.
What, then, must be done to allow migrate
to add the 5 MySQL tables?
Result:
mysql> use diet_py;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_diet_py |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
Following Django tutorial documentation, with slight modifications, I have these directories & files:
Tree:
...DB1-PROJECT
│ db.sqlite3
│ manage.py
│
├───diet
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ ├───migrations
│ │ │ __init__.py
│ │ │
│ │ └───__pycache__
│ │ __init__.cpython-311.pyc
│ │
│ └───__pycache__
│ admin.cpython-311.pyc
│ apps.cpython-311.pyc
│ models.cpython-311.pyc
│ __init__.cpython-311.pyc
│
└───mysite
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└───__pycache__
settings.cpython-311.pyc
urls.cpython-311.pyc
wsgi.cpython-311.pyc
__init__.cpython-311.pyc
..\diet\models.py
from django.db import models
# Create your models here.
from django.db import models
class Food(models.Model):
food_name = models.CharField(max_length=255)
class Meta:
db_table = 'food'
class Gut(models.Model):
description = models.CharField(max_length=255, blank=True, null=True)
datetime = models.DateTimeField()
reaction_id = models.IntegerField()
class Meta:
db_table = 'gut'
class Meal(models.Model):
meal_type = models.CharField(max_length=255)
date = models.DateTimeField()
class Meta:
db_table = 'meal'
class MealFood(models.Model):
meal = models.OneToOneField(Meal, models.DO_NOTHING, primary_key=True)
food = models.ForeignKey(Food, models.DO_NOTHING)
class Meta:
db_table = 'meal_food'
unique_together = (('meal', 'food'),)
class Reaction(models.Model):
reaction = models.CharField(max_length=45)
class Meta:
db_table = 'reaction'
...\mysite\settings.py
from pathlib import Path
import pymysql
pymysql.install_as_MySQLdb()
...
INSTALLED_APPS = [
'diet.apps.DietConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'diet_py',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
...
after model code finished. you need to run makemigrations command in CLI
python manage.py makemigrations
When you run this command you can see the modifications and additions that you are created in the command line some thing like following
diet/migrations/0001_initial.py
- Create model Food
if that not worked. The try the migrate command with app name as parameter like bellow
python manage.py makemigrations diet