简体   繁体   English

“python manage.py syncdb”没有创建表

[英]“python manage.py syncdb” not creating tables

I first ran 我先跑了

python manage.py syncdb

and it created the database and tables for me, then I tried to add more apps, and here's what I did: 它为我创建了数据库和表格,然后我尝试添加更多应用程序,这就是我所做的:

create apps by 创建应用程序

python manage.py startapp newapp

Then I added 'newapp' to INSTALLED_APPS in setting.py: 然后我在'newapp'添加到INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'newapp',
)

At last I ran syncdb : 最后我运行了syncdb

python manage.py syncdb

and here's the result I get: 这是我得到的结果:

Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

I checked my db and there is no table named newapp , no table's name including newapp . 我检查了我的数据库,没有名为newapp表,没有包括newapp表名。

I also ran into this issue, and was able to get around it by removing the migrations folder inside my app. 我也遇到了这个问题,并且能够通过删除我的应用程序中的迁移文件夹来绕过它。 Somehow that had already gotten created and was tricking syncdb into thinking that it was fully migrated already, but those migration scripts didn't actually do anything useful. 某种程度上已经创建并且诱使syncdb认为它已经完全迁移,但是这些迁移脚本实际上没有做任何有用的事情。 Obviously don't try this if you actually have migrations you want to save, but I was working with a brand new app and models. 显然,如果你实际上有想要保存的迁移,请不要尝试这个,但我正在使用一个全新的应用程序和模型。

I tried most of the ideas above: 我尝试了上面的大部分想法:

  • making sure the models.py is imported (verified that the module executed during a makemigrate), 确保导入models.py (验证在makemigrate期间执行的模块),
  • deleting the migrations folder 删除migrations文件夹
  • setting managed = True (this is default anyways), 设置managed = True (这是默认值),
  • used the python manage.py inspectdb (which correctly dumped the table, if I had created it manually). 使用了python manage.py inspectdb (正确地转储了表,如果我手动创建的话)。

The key was simply to run a makemigrations on the app separately: 关键是单独在应用程序上运行makemigrations

python manage.py makemigrations <app_name>

as part of performing the makemigrations step. 作为执行makemigrations步骤的一部分。 Then you do 然后你做

python manage.py migrate

afterwards as usual. 之后像往常一样。

(applies to Django 1.10, using Postgres 9.5). (适用于Django 1.10,使用Postgres 9.5)。

Django documentation Django文档

Credit, related post 信用,相关帖子

If you run: 如果您运行:

python manage.py inspectdb > somefile.txt

You can get quickly check out if your database structure is matching your django models. 如果您的数据库结构与您的django模型匹配,您可以快速查看。

i got same problem, but i didn't have any "migration" folder. 我有同样的问题,但我没有任何“迁移”文件夹。 I solved it like below. 我解决了下面的问题。

I just added app_label with the model code, 我刚刚在app_label添加了模型代码,

class MyModel(models.Model):
...
    class Meta:
        managed = True      # add this
        app_label = 'myapp' # & this

Also, make sure it is discoverable by referencing it in myapp/models/__init__.py 另外,通过在myapp/models/__init__.py引用它,确保它是可发现的

from model_file.py import MyModel

I was having the same problem and noticed that it had created a db.sqlite3 file that didn't seem empty: 我有同样的问题,并注意到它创建了一个似乎不是空的db.sqlite3文件:

$ ls -l
-rw-r--r--   1 sauron  staff  122880 Jul 31 01:22 db.sqlite3

I tried running sqlite again using the filename as an argument and that worked: 我尝试使用文件名作为参数再次运行sqlite,并且工作正常:

$ sqlite3 db.sqlite3 
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "auth_group" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(80) NOT NULL UNIQUE
);
... many rows ...

You should be using this command 您应该使用此命令

python manage.py migrate

where you were runing syncdb from the location where manage.py resides 您从manage.py所在的位置运行syncdb的位置

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

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