当模型名称类似于文件名时,Django模型不生成sql?

时间:2013-12-07 10:16:46

标签: sql django

我正在写一个名为评论的django应用,但是......我使用模型评论时出错。

我运行以下命令,发现注释不生成sql。

python manage.py sqlall comments

输出的一部分:

BEGIN;
CREATE TABLE "django_comments" (
    "id" integer NOT NULL PRIMARY KEY,
    "content_type_id" integer NOT NULL REFERENCES "django_content_type" ("id"),
    "object_pk" text NOT NULL,
    "site_id" integer NOT NULL REFERENCES "django_site" ("id"),
    "user_id" integer REFERENCES "auth_user" ("id"),
    "user_name" varchar(50) NOT NULL,
    "user_email" varchar(75) NOT NULL,
    "user_url" varchar(200) NOT NULL,
    "comment" text NOT NULL,
    "submit_date" datetime NOT NULL,
    "ip_address" char(15),
    "is_public" bool NOT NULL,
    "is_removed" bool NOT NULL
)
;
CREATE TABLE "django_comment_flags" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
    "comment_id" integer NOT NULL REFERENCES "django_comments" ("id"),
    "flag" varchar(30) NOT NULL,
    "flag_date" datetime NOT NULL,
    UNIQUE ("user_id", "comment_id", "flag")
)
;
...

CREATE TABLE "comments_url" (
    "id" integer NOT NULL PRIMARY KEY,
    "content" varchar(64) NOT NULL,
    "url" varchar(2048) NOT NULL
)
;
CREATE TABLE "comments_commentboard" (
    "id" integer NOT NULL PRIMARY KEY,
    "title" varchar(64) NOT NULL,
    "url" varchar(2048) NOT NULL
)
;
CREATE INDEX "django_comments_37ef4eb4" ON "django_comments" ("content_type_id")
;
CREATE INDEX "django_comments_99732b5c" ON "django_comments" ("site_id");
CREATE INDEX "django_comments_6340c63c" ON "django_comments" ("user_id");
CREATE INDEX "django_comment_flags_6340c63c" ON "django_comment_flags" ("user_id
");
CREATE INDEX "django_comment_flags_3925f323" ON "django_comment_flags" ("comment
_id");
CREATE INDEX "django_comment_flags_9f00eb17" ON "django_comment_flags" ("flag");


COMMIT;

models.py的一部分:

from django.db import models
from django.contrib.auth.models import User
import datetime

...

class Url(models.Model):
    content = models.CharField(max_length=64)
    url = models.URLField(max_length=2048)

    def __unicode__(self):
        return self.content

class Comment(models.Model):
    title = models.CharField(max_length=64)
    comment_str = models.CharField(max_length=128)
    #desc = models.CharField(max_length=64)
    #comment_board = models.ForeignKey(CommentBoard)

    #def __unicode__(self):
    #    return self.title + ':' + self.content

class CommentBoard(models.Model):
    title = models.CharField(max_length=64)
    url = models.URLField(max_length=2048)

    def __unicode(self):
        return self.title

当我将Comment重命名为Comment123时,它就可以了。但我想这是一个限制 我以前不知道。谁能告诉我为什么会这样?

CREATE TABLE "comments_url" (
    "id" integer NOT NULL PRIMARY KEY,
    "content" varchar(64) NOT NULL,
    "url" varchar(2048) NOT NULL
)
;
CREATE TABLE "comments_comment123" (
    "id" integer NOT NULL PRIMARY KEY,
    "title" varchar(64) NOT NULL,
    "comment_str" varchar(128) NOT NULL
)
;
CREATE TABLE "comments_commentboard" (
    "id" integer NOT NULL PRIMARY KEY,
    "title" varchar(64) NOT NULL,
    "url" varchar(2048) NOT NULL
)
;

我不应该同时使用评论作为app-name和评论模型吗?

谁能告诉我在哪里可以找到限制?

1 个答案:

答案 0 :(得分:0)

模型名称是否与应用名称相似无关紧要。

您的INSTALLED_APPS设置中的应用必须是唯一的。如果'django.contrib.comments'中有INSTALLED_APPS,则您无法为自己的应用comments命名。