SQLAlchemy:带有load_only,order_by和limit的无效SQL

时间:2015-02-23 21:02:53

标签: python postgresql sqlalchemy postgis geoalchemy2

运行以下示例时,会生成以下无效的SQL查询:

SELECT anon_1.venue_id              AS anon_1_venue_id, 
    St_asbinary(anon_1.venue_location)  AS anon_1_venue_location, 
    St_asbinary(anon_1.anon_2)      AS anon_1_anon_2, 
    label_1.id              AS label_1_id 
FROM    (
    SELECT venue.id AS venue_id, 
               venue.location AS venue_location, 
               venue.location <-> St_geomfromtext(:ST_GeomFromText_1, 
                  :ST_GeomFromText_2) AS anon_2 
    FROM   venue 
    ORDER  BY venue.location <-> St_geomfromtext(:ST_GeomFromText_1, 
                     :ST_GeomFromText_2) 
    LIMIT  :param_1
    ) AS anon_1 
LEFT OUTER JOIN (
    venue_to_label AS venue_to_label_1 
    JOIN label AS label_1 
    ON label_1.id = venue_to_label_1.label_id) 
ON anon_1.venue_id = venue_to_label_1.venue_id 
ORDER  BY anon_1.anon_2

问题是St_asbinary应用于anon_1.anon_2。我希望这条线要么不生成,要么至少没有&#34; St_asbinary&#34;。我很确定这是GeoAlchemy2的错。任何人都可以评论这个假设吗?

知道如何最好地解决这个问题吗?不幸的是,它似乎非常基础。我们正在尝试在大项目中使用代码,欢迎任何帮助!

下面的(最小)示例假定本地PostgreSQL数据库&#34; tmp&#34;已安装GIS扩展程序。

import unittest
from geoalchemy2 import WKTElement, Geometry
from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import joinedload, relationship, load_only
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = (
    'postgres://postgres:password@localhost:5432/tmp')
db = SQLAlchemy(app)

Base = declarative_base()

# many (venue) <-> many (label) mapping table
venue_to_label = Table(
    'venue_to_label', db.metadata,
    Column('venue_id', Integer, ForeignKey('venue.id'), primary_key=True),
    Column('label_id', Integer, ForeignKey('label.id'), primary_key=True)
)


class Label(db.Model):
    __tablename__ = 'label'
    id = Column(Integer, primary_key=True, nullable=False)


class Venue(db.Model):
    id = Column(Integer, primary_key=True, nullable=False)
    labels = relationship(Label, secondary=venue_to_label)
    location = Column(Geometry(geometry_type="POINT"), nullable=False)

db.create_all()


class TestGeoAlchemy2Bug(unittest.TestCase):

    def test_geo_alchemy2_bug(self):
        point = WKTElement("POINT(0 0)")

        query = Venue.query
        query = query.options(joinedload(*['labels']).load_only(*['id']))
        query = query.order_by(Venue.location.distance_centroid(point))
        query = query.limit(10)

        print query
        print query.all()

免责声明:我已经在GeoAlchemy2 github页面上将此问题作为问题发布,但尚未得到任何回复(https://github.com/geoalchemy/geoalchemy2/issues/93)。

即使是一些一般的建议,我应该看到哪个方向非常感谢!

感谢您的帮助!


更新

现在通过创建&#34;缺失&#34;解决了这个问题。使用以下命令创建数据库时的函数:

CREATE FUNCTION St_asbinary(double precision)
    RETURNS double precision
AS 'select $1;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT; 

对正确的解决方案仍然非常感兴趣!

1 个答案:

答案 0 :(得分:0)

此问题已在最新版本中得到解决! https://github.com/geoalchemy/geoalchemy2/issues/93