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

发布于 2024-10-31 11:55:47 字数 812 浏览 2 评论 0原文

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

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 函数文件方法,而不是模板方法)

提前欢呼以获得任何帮助

这是由空模式,并且表是从头开始创建的

I've created a Django project, using a PostGIS backend, which all seems well as far as I can tell. I've now created an app in this project as follows:

from django.contrib.gis.db import models

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

    objects = models.GeoManager()

This should be a simple model for a name/point pair (to be built upon later), but the database is not creating the location field. There is no error, and the field is ignored until I try to access the model (using the Admin) where it croaks with "DatabaseError: column myproj_myplace.location does not exist"

I'm really not sure what's going wrong here, since there is no error on parsing (syncdb works without exception). When I ran the sql command for the model, this is what I got:

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

Again no fields! Any ideas?

I have both 'django.contrib.gis' and my app in my INSTALLED_APPS, and have built my database schema with PostGIS support (adding the sql functions file method, not the template method)

Cheers in advance for any assistance

This is done from an empty schema, and the table is created from scratch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-11-07 11:55:47

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

Try 'python manage.py sqlall {app}', the sql output doesn't include spatial columns for some reason but sqlall does. Then try running the output directly on the database using pgadmin3, you'll likely get a clearer idea of what's going wrong.

跨年 2024-11-07 11:55:47

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

例如使用mysql删除表和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 表仍然是最有可能的问题。

Django doesn't perform migrations. Look at using South for migrations, or manually drop the table and resync to rebuild the table.

For example using mysql dropping table and 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 ...

edit:

oops you are using the postgresql probably. someone else might be able to give you an example, but an existing myplace table in your database is still the most likely the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文