简体   繁体   English

python manage.py migrate 没有创建表

[英]python manage.py migrate does not create a table

I use postgresql and my python version is 3.7 I have tried all the commands我使用 postgresql 我的 python 版本是 3.7 我已经尝试了所有的命令

python manage.py makemigrations

This creates a file in migrations named 0001_initial.py这会在名为 0001_initial.py 的迁移中创建一个文件

# Generated by Django 2.2.10 on 2020-02-17 12:01

from django.db import migrations, models从 django.db 导入迁移、模型

class Migration(migrations.Migration): class 迁移(迁移。迁移):

initial = True

dependencies = [
]

operations = [
    migrations.CreateModel(
        name='Support',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('name', models.CharField(max_length=40)),
            ('email', models.EmailField(max_length=254)),
            ('message', models.TextField()),
        ],
    ),
]

now when I pass the command to migrate,现在当我通过迁移命令时,

python manage.py migrate

It shows that 'No migrations to apply'它显示“没有要申请的迁移”

    (myvenv) C:\nid\project>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, scholarship, sessions
Running migrations:
  No migrations to apply.

This is my models.py class:这是我的models.py class:

    from django.db import models
from django.utils import timezone

# Create your models here.


class Support(models.Model):
    name = models.CharField(max_length=40)
    email = models.EmailField()
    message = models.TextField()

    def __str__(self):
        return self.name

I know that this question has been asked before.我知道以前有人问过这个问题。 I checked them all but no answer is able to solve this problem.我检查了所有但没有答案能够解决这个问题。 Please help请帮忙

In general, this means that the table already exists in your DB.通常,这意味着该表已存在于您的数据库中。 I suggest that you try to delete your table and then recreate it with py manage.py migrate .我建议您尝试删除您的表,然后使用py manage.py migrate重新创建它。

Reply if you have further problems如果您有其他问题,请回复

有点旧的帖子,但我刚刚遇到了同样的问题,只需运行即可解决:
python manage.py makemigrations
python manage.py migrate --run-syncdb

So what happens behind the scenes is:那么幕后发生的事情是:

When you run the command: python manage.py makemigrations运行命令时: python manage.py makemigrations

A migration file gets created based on your model or model changes.根据您的 model 或 model 更改创建迁移文件。

When you run: python manage.py migrate运行时: python manage.py migrate

Django uses the newly created file inside the migrations folders and performs the actions accordingly AND a corresponding entry is created in the table django_migrations (sort of a log). Django 使用迁移文件夹中新创建的文件并执行相应的操作,并在表django_migrations (某种日志)中创建相应的条目。

Now even if you DELETE the table from DB and the migration file from the folder, the migrate command will not work because the new file created will always be named '0001_initial.py' as you have deleted the previous migration file.现在,即使您从数据库中删除表并从文件夹中删除迁移文件,迁移命令也不会起作用,因为创建的新文件将始终命名为“0001_initial.py”,因为您已经删除了以前的迁移文件。

If you explore the table 'django_migrations' you will find a entry with your app_name and migrations file name.如果您浏览表“django_migrations”,您将找到一个包含您的 app_name 和迁移文件名的条目。 Example:例子:

myApp 0001_initial我的应用程序 0001_initial

Django will always assume that the migration has already run before and will not run it again. Django 会一直假设迁移之前已经运行过,不会再运行。

How to fix?怎么修?

Option 1: python manage.py migrate --run-syncdb选项一: python manage.py migrate --run-syncdb

Option 2: Delete the particular entry that's conflicting with your last migration filename / or the one you want to run / or all of them related to that app选项 2:删除与您上次迁移文件名冲突的特定条目/或您要运行的文件名/或所有与该应用程序相关的条目

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

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