简体   繁体   English

如何在 MySQL 中创建模型表?

[英]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.我正在尝试在 Python/Django 中复制一个 PHP/Symfony 项目作为 python 学习练习。 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{...} . Platform = Windows 10. 预期结果是migrate命令将添加与settings.py INSTALLED_APPS{...}中所有条目相关的表。 Instead, the migrate command adds all Django tables but none of the tables of models.py .相反, migrate命令添加了所有Django表,但没有添加models.py的任何表。

What, then, must be done to allow migrate to add the 5 MySQL tables?那么,必须做什么才能允许migrate添加 5 个 MySQL 表?

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:按照 Django 教程文档,稍作修改,我有这些目录和文件:

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. model 代码完成后。 you need to run makemigrations command in CLI你需要在 CLI 中运行 makemigrations 命令

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM