python - PostgreSQL - 无法创建表 - 数据类型点没有访问方法 "btree"的默认运算符类

标签 python sql postgresql psycopg2

所以我正在做一个项目来学习 SQL、Python 和一些服务器端的东西,经过大量搜索后,我在这一点上很困惑。我想创建两个相同的表,我有以下代码:

import psycopg2 as db
import json

with open('config.json', 'r') as f:
config = json.load(f)

conn = db.connect(user=config['dbuser'], database=config['dbname'], host=config['dbhost'], password=config['dbpass']);
cur = conn.cursor()

cur.execute("""CREATE TABLE IF NOT EXISTS A 
    (
            city            TEXT,
            location        POINT NOT NULL,
            eta             INTEGER,
            time            TIMESTAMP WITH TIME ZONE NOT NULL,
            holiday         BOOLEAN,
            PRIMARY KEY (location, time)
    );""")

还有表B,格式相同,其中A和B是不同的服务。运行它时,我得到:

Traceback (most recent call last):
  File "dbsetup.py", line 18, in <module>
);""")
psycopg2.ProgrammingError: data type point has no default operator class for access method "btree"
HINT:  You must specify an operator class for the index or define a default operator class for the data type.

我现在已经搜索了一段时间,但我遇到的任何东西都与这个案例非常相似,而且我很困惑,因为我肯定在这里遗漏或误解了一些非常基本的东西。任何帮助/指向正确方向的帮助将不胜感激!一般建议也非常受欢迎。

最佳答案

PostgreSQL 版本不支持在 point 数据类型上创建普通或唯一的 b-tree 索引(至少在 9.5 之前是正确的)。因此,您不能将 point 用作 PRIMARY KEY 的一部分。

test=> CREATE TABLE test_table( xy point primary key );
ERROR:  data type point has no default operator class for access method "btree"
HINT:  You must specify an operator class for the index or define a default operator class for the data type.

您需要更改数据模型,这样您就不会尝试将用作 PK 的一部分。

或者您可以编写一个扩展来为 point 类型添加 b-tree 索引支持,但这需要很多更多的工作和理解 PostgreSQL 的索引工作。

无论如何,如果您正在做地理方面的工作,您可能需要考虑使用 PostGISgeometry 数据类型。它具有更丰富的搜索操作集,可以查找最近的点、按距离查找、检查点是否在区域内等,所有这些都可以有效地建立索引。

关于python - PostgreSQL - 无法创建表 - 数据类型点没有访问方法 "btree"的默认运算符类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558584/

相关文章:

sql - 是否可以通过sql脚本获取Azure数据库区域?

sql - Postgres : aggregation function that returns a column

python - psycopg2 在我造成问题的字符串周围添加引号

database - Postgres : How to create reference cell?

python - 连分数 Python

Python 启动可执行文件然后 TCP 连接到进程

python - 使用 python 中的列表理解更改列表值

python - 无法保存 Django 表单 : 'UserProfile' object has no attribute 'save_m2m'

sql - Oracle 11g 上的 select 子句中的双/随机别名不会引发无效标识符异常

php - MySQL 如果存在更新值,否则插入