Django - DatabaseError:没有这样的表 [英] Django - DatabaseError: No such table

查看:146
本文介绍了Django - DatabaseError:没有这样的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了两个模型:

  class Server(models.Model):
owner = models.ManyToManyField 'Person')

class Person(models.Model):
name = models.CharField(max_length = 50)

admin.site.register
admin.site.register(Person)

之后,我甚至检查了sql,just为乐趣:

  BEGIN; 
CREATE TABLEservers_server_owners(
idinteger NOT NULL PRIMARY KEY,
server_idinteger NOT NULL,
person_idinteger NOT NULL,
UNIQUE (server_id,person_id)

;
CREATE TABLEservers_server(
idinteger NOT NULL PRIMARY KEY,
namevarchar(50)NOT NULL,
portinteger unsigned NOT NULL,
stateinteger NOT NULL

;
CREATE TABLEservers_person(
idinteger NOT NULL PRIMARY KEY,
namevarchar(50)NOT NULL

;
COMMIT;

还有 CREATE TABLEservers_server_owners



我运行 syncdb 将新模型安装到数据库。我去管理界面定义一些对象,但我得到以下错误:

  DatabaseError at / admin / servers / server / 1 / 
没有这样的表:servers_server_owners

服务器,再次运行 syncdb 启动服务器:仍然有同样的问题。为什么不能找到,表,即使它只是告诉我它创建了id?

解决方案

该表从未创建。因为我是django相当新的,我不知道 ./ manage.py syncdb 不更新现有模型,而是只创建不存在的模型。



因为模型'Server'在我添加其他模型之前就存在,并且它已经在db中,'syncdb'实际上没有创建新表。 p>

I defined two models:

class Server(models.Model):
    owners = models.ManyToManyField('Person')

class Person(models.Model):
    name = models.CharField(max_length=50)

admin.site.register(Server)
admin.site.register(Person)

After that I even checked the sql, just for fun:

BEGIN;
CREATE TABLE "servers_server_owners" (
    "id" integer NOT NULL PRIMARY KEY,
    "server_id" integer NOT NULL,
    "person_id" integer NOT NULL,
    UNIQUE ("server_id", "person_id")
)
;
CREATE TABLE "servers_server" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL,
    "port" integer unsigned NOT NULL,
    "state" integer NOT NULL
)
;
CREATE TABLE "servers_person" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL
)
;
COMMIT;

There it even says CREATE TABLE "servers_server_owners"

I ran syncdb to install the new models to the database. I went to the admin-interface to define some objects to play with, but I got the following error:

DatabaseError at /admin/servers/server/1/  
no such table: servers_server_owners

I shutdown the dev-server, ran syncdb again, started the server: Still same problem. Why can't it find, the table, even though it just told me it created id?

解决方案

Actually the problem was that the table never got created. Since I am fairly new with django, I did not know that ./manage.py syncdb does not update existing models, but only creates the ones that do not exist.

Because the model 'Server' existed before I added the other model, and it was already in the db, 'syncdb' did not actually create the new tables.

这篇关于Django - DatabaseError:没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆