Python Django 迁移:不创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35494035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:31:23  来源:igfitidea点击:

Django migrate : doesn't create tables

pythonmysqldjangomigration

提问by Adam Cherti

After some errors, I dropped my database, deleted all my migration files (I left init.py). Now, when I run

在一些错误之后,我删除了我的数据库,删除了我所有的迁移文件(我留下了 init.py)。现在,当我跑

python migrate.py makemigrations   // It creates migrations correctly
python migrate.py migrate          // It outputs "app.0001_initial OK"

But absolutely NOtable(related to my app) is created. Only those related to django are. And in the migration table, my application migration is marked is done but no table have been created as I said, it's very displeasing.

但绝对NO(与我的应用程序)时创建的。只有那些与 django 相关的。在迁移表中,我的应用程序迁移被标记为已完成但没有像我所说的那样创建表,这非常令人不快。

Here is an excerpt my migration file :

这是我的迁移文件的摘录:

# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-02-18 21:59
from __future__ import unicode_literals

import colorful.fields
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Client',
            fields=[
                ('id', models.AutoField(db_column='idtblclients', primary_key=True, serialize=False)),
                ('genre1', models.CharField(blank=True, max_length=10)),
            ('prenom1', models.CharField(blank=True, max_length=45)),
            ('nom1', models.CharField(blank=True, max_length=45)),
            ('genre2', models.CharField(blank=True, max_length=10)),
            ('prenom2', models.CharField(blank=True, max_length=45)),
            ('nom2', models.CharField(blank=True, max_length=45)),
            ('courriel', models.CharField(blank=True, max_length=45)),
            ('langue', models.CharField(blank=True, max_length=1)),
            ('numtel1', models.CharField(blank=True, db_column='NumTel1', max_length=20)),
            ('numtel2', models.CharField(blank=True, db_column='NumTel2', max_length=20)),
            ('numcivique', models.CharField(blank=True, db_column='NumCivique', max_length=15)),
            ('rue', models.CharField(blank=True, db_column='Rue', max_length=45)),
            ('ville', models.CharField(blank=True, db_column='Ville', max_length=45)),
            ('codepostal', models.CharField(blank=True, db_column='CodePostal', max_length=45)),
            ('timestamp', models.DateTimeField(blank=True, db_column='Timestamp', null=True)),
            ('zone', models.CharField(blank=True, db_column='Zone', max_length=45)),
        ],
        options={
            'db_table': 'tblclients',
            'managed': False,
        },
    ),
....

Do you have an idea how to fix it ?

你知道如何解决它吗?

采纳答案by JulienD

From Django docs, Options.managed: "If False, no database table creation or deletion operations will be performed for this model."

来自 Django 文档,Options.managed:“如果为 False,则不会为此模型执行数据库表创建或删除操作。”

And I see you have

我看到你有

   options={
        'db_table': 'tblclients',
        'managed': False,
    },

Try setting managed=Truein the model.

尝试managed=True在模型中设置。

回答by Vignesh

python manage.py migrate --fake APPNAME zero

This will make your migration to fake. Now you can run the migrate script

这将使您的迁移成为假的。现在您可以运行迁移脚本

python manage.py migrate APPNAME

Tables will be created and you solved your problem.. Cheers!!!

将创建表格,您解决了您的问题..干杯!!!

回答by Rajiv Sharma

I face same issue:

我面临同样的问题:

python manage.py migrate poll
Operations to perform:
  Apply all migrations: poll
Running migrations:
  No migrations to apply.

Follow these steps to create tables for your app.

按照以下步骤为您的应用程序创建表。

Go to your app folder

1.delete migration folder.

2.python manage.py makemigrations.

3 Rename 0001_initial.py file to 0001_initial_manual.py.

4 python manage.py migrate APPNAME.

转到您的应用程序文件夹

1.删​​除迁移文件夹。

2.python manage.py makemigrations。

3 将 0001_initial.py 文件重命名为 0001_initial_manual.py。

4 python manage.py 迁移APPNAME。

After this my tables created smoothly.

在此之后,我的表创建顺利。

python manage.py migrate poll
Operations to perform:
  Apply all migrations: poll
Running migrations:
  Applying poll.0002_initial... OK

回答by Roddy P. Carbonell

In my case, what created the tables was this:

就我而言,创建表的原因是:

python manage.py migrate --run-syncdb

I am using Django 1.9.6.

我正在使用 Django 1.9.6。

回答by Geordie

This is how I got @JulienD's answer to work:

这就是我得到@JulienD 的工作答案的方式:

  1. I added metadata to my model def in models.py:
  1. 我在models.py中将元数据添加到我的模型定义中:

class thing(models.Model): name = models.CharField(max_length=200) class Meta: app_label = 'things' managed = True

class thing(models.Model): name = models.CharField(max_length=200) class Meta: app_label = 'things' managed = True

  1. Execute:
  1. 执行:

python manage.py makemigrations

python manage.py makemigrations

  1. Execute:
  1. 执行:

python manage.py migrate

python manage.py migrate

After that python manage.py showmigrationsshows the migrations and the admin site started working.

之后python manage.py showmigrations显示迁移和管理站点开始工作。

回答by Durai

Please check if you are using multiple databases in your project. If multiple databases configured, check the default database is the one you are looking for.

请检查您的项目中是否使用了多个数据库。如果配置了多个数据库,请检查默认数据库是否是您要查找的数据库。

If default and your expected database are different, run below command pointing to your db configuration.

如果默认数据库和您预期的数据库不同,请在指向您的数据库配置的命令下运行。

python manage.py migrate $app_name --database $dbname_from_setting

python manage.py migrate $app_name --database $dbname_from_setting

Hope this helps.

希望这可以帮助。

回答by Doug Nintzel

The answer to this depends if you already have a history of migrations in your app folder. In my case I didn't...I think I deleted original migration scripts.

答案取决于您的应用程序文件夹中是否已经有迁移历史记录。就我而言,我没有......我想我删除了原始迁移脚本。

So following the Django docs, I did the following:

因此,按照 Django 文档,我执行了以下操作:

  1. Comment out new updates in models.py
  2. Follow section Adding migrations to apps...
  3. Create migrations for what is already existing:

    python manage.py makemigrations yourapp

  4. Do fake applying of these migrations (below singles that the tables already exist and not to try to recreate):

    python manage.py migrate --fake-initial

  5. Uncomment changes in model.py
  6. Follow steps in section Workflow...

    python manage.py makemigrations yourapp

    python manage.py migrate

  1. 注释掉 models.py 中的新更新
  2. 按照部分添加迁移到应用程序...
  3. 为已经存在的内容创建迁移:

    python manage.py makemigrations yourapp

  4. 对这些迁移进行假应用(在表已经存在的单曲下面,不要尝试重新创建):

    python manage.py migrate --fake-initial

  5. 取消注释 model.py 中的更改
  6. 按照工作流程...部分中的步骤进行操作

    python manage.py makemigrations yourapp

    python manage.py 迁移

回答by ben othman zied

Delete folder migrations and re-run. It works for me

删除文件夹迁移并重新运行。这个对我有用

回答by 7guyo

First try this;

先试试这个;

Check your migration files for the app, if you have the create modelportion in your migrations file, remove it and redo migrations. I.e remove this; migrations.CreateModel( name='YourModelName', ....... .....)

检查应用程序的迁移文件,如果迁移文件中有创建模型部分,请将其删除并重做迁移。即删除这个; migrations.CreateModel( name='YourModelName', ....... ....)

(venv)root@debian:~/Desktop/project$ python manage.py makemigrations appName
(venv)root@debian:~/Desktop/project$ python manage.py migrate appName

Else; navigate to your database and delete all migrations for that app, you may have to drop the related tables too.

别的; 导航到您的数据库并删除该应用程序的所有迁移,您可能也必须删除相关表。

postgres=# \c db_yourdb
db_yourdb=# delete from django_migrations where app='appName';

then go to your code and;

然后转到您的代码和;

(venv)root@debian:~/Desktop/project$ python manage.py makemigrations appName
(venv)root@debian:~/Desktop/project$ python manage.py migrate appName

To do fresh migrations entirely, first do the necessary deletions for migrations and droping the tables if need be then;

要完全进行新迁移,首先要对迁移进行必要的删除,然后在需要时删除表;

(venv)root@debian:~/Desktop/project$ find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
(venv)root@debian:~/Desktop/project$ find . -path "*/migrations/*.pyc"  -delete

finally do;

终于做到了;

(venv)root@debian:~/Desktop/project$ python manage.py makemigrations
(venv)root@debian:~/Desktop/project$ python manage.py migrate

Read more here on how to reset migrations

在此处阅读有关如何重置迁移的更多信息