Author: Xu FC
Django: 3.0.2
Python: 3.7.0
創(chuàng)建 Table
- 在 APP 中的
models.py
添加class
from django.db import models
class product_table(models.Model):
product_name = models.CharField(max_length=20, editable=True)
product_description = models.TextField(null=True)
- 同步數(shù)據(jù)庫(kù)
python3 manage.py makemigrations
python3 manage.py migrate
- 查看 Table
MariaDB [TestManager]> show tables;
+----------------------------+
| Tables_in_TestManager |
+----------------------------+
| Manage0_product_table |
| 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 |
+----------------------------+
11 rows in set (0.001 sec)
- 查看表結(jié)構(gòu)
MariaDB [TestManager]> desc Manage0_product_table;
+---------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| product_name | varchar(20) | NO | | NULL | |
| product_description | longtext | YES | | NULL | |
+---------------------+-------------+------+-----+---------+----------------+
3 rows in set (0.009 sec)
- 通過(guò) django 提供的 API 插入和查詢數(shù)據(jù)
? MProject python3 manage.py shell
>>> from Manage0.models import verified
>>> verified.objects.create(bugID='83128', product='ASF', version='1.0.2', tester='lihw', pm='xufc', ad
d_date='2020-01-19')
<verified: 83128>
>>> verified.objects.all()
<QuerySet [<verified: 84405>, <verified: 84732>, <verified: 83128>]>
>>>
刪除 Table
- 將要?jiǎng)h除的 table 對(duì)應(yīng)的
class
刪掉或者注釋掉 - 同步數(shù)據(jù)庫(kù)
python3 manage.py makemigrations
python3 manage.py migrate
問題解決
問題:不小心先刪掉了數(shù)據(jù)庫(kù)中的表
- 不小心先刪掉了數(shù)據(jù)庫(kù)中的表
- 解決辦法:
- 注釋掉相應(yīng)的代碼鳖悠;
- 偽同步數(shù)據(jù)庫(kù)
python3 manage.py makemigrations
python3 manage.py migrate --fake
問題:django.db.utils.ProgrammingError: (1146, "xx doesn't exist")
django.db.utils.ProgrammingError: (1146, "Table 'TestManager.django_content_type' doesn't exist")
- 解決辦法:
- 刪除 APP 目錄下 migrations 目錄下的除
__init__.py
的所有文件; - 刪除數(shù)據(jù)庫(kù)中
django_migrations
數(shù)據(jù)表优妙; - 重新同步
- 刪除 APP 目錄下 migrations 目錄下的除
python3 manage.py makemigrations
python3 manage.py migrate