GeoDjango项目缺少数据库模式中的字段

时间:2011-04-08 14:43:16

标签: django postgresql django-models postgis geodjango

我使用PostGIS后端创建了一个Django项目,就我所知,这一切似乎都很好。我现在在这个项目中创建了一个应用程序,如下所示:

from django.contrib.gis.db import models

class MyPlace(models.Model):
    name = models.CharField(max_length=255)
    location = models.PointField()

    objects = models.GeoManager()

这应该是名称/点对的简单模型(稍后构建),但数据库不创建位置字段。没有错误,该字段被忽略,直到我尝试访问模型(使用管理员),其中它与“DatabaseError:列myproj_myplace.location不存在”结合在一起

我真的不确定这里出了什么问题,因为解析时没有错误(syncdb无例外地工作)。当我为模型运行sql命令时,这就是我得到的:

BEGIN;
CREATE TABLE "myproj_myplace" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(255) NOT NULL
)
;
COMMIT;

再次没有字段!有什么想法吗?

我的INSTALLED_APPS中有'django.contrib.gis'和我的应用程序,并且已经使用PostGIS支持构建了我的数据库模式(添加了sql函数文件方法,而不是模板方法)

提前为任何帮助干杯

这是从空架构完成的,表是从头开始创建的

2 个答案:

答案 0 :(得分:2)

尝试'python manage.py sqlall {app}',sql输出由于某种原因不包含空间列,但sqlall会这样做。然后尝试使用pgadmin3直接在数据库上运行输出,你可能会更清楚地知道出了什么问题。

答案 1 :(得分:0)

Django不执行迁移。查看使用South进行迁移,或手动删除表并重新同步以重建表。

例如使用mysql drop table和syncdb:

$ mysql -u myprojadmin -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19215
Server version: 5.1.49-1ubuntu8.1 (Ubuntu)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use myproj;

Database changed
mysql> drop table myproj_myplace;
Query OK, 0 rows affected (0.02 sec)

mysql> exit;
Bye
$ ./manage.py syncdb
Syncing...
Creating tables ...

修改

你可能正在使用postgresql。其他人可能会给你一个例子,但数据库中现有的myplace表仍然是最有可能的问题。