Python 与sqlalchemy中的SQL限制和偏移混淆

Python 与sqlalchemy中的SQL限制和偏移混淆,python,sqlalchemy,Python,Sqlalchemy,各位,我在使用烧瓶和炼金术时遇到了一些问题 我有三种型号: tag_article_table = db.Table('tag_article_table', db.Column('article_id', db.Integer, db.ForeignKey('article.id')), db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')) ) class Article(db.Model): __table

各位,我在使用烧瓶和炼金术时遇到了一些问题

我有三种型号:

tag_article_table = db.Table('tag_article_table',
    db.Column('article_id', db.Integer, db.ForeignKey('article.id')),
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'))
)

class Article(db.Model):
    __tablename__ = 'article'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String)
    content = db.Column(db.Text)
    cover_image = db.Column(db.String)
    url_title = db.Column(db.String)
    seo_keyword = db.Column(db.String)
    seo_description = db.Column(db.Text)
    author = db.Column(db.String)
    created_date = db.Column(db.Date)
    views_count = db.Column(db.Integer)
    comments_count = db.Column(db.Integer)
    catalog_id = db.Column(db.Integer, db.ForeignKey('channel.id'))
    tags = db.relationship("Tag", secondary=tag_article_table, backref="articles")

class Channel(db.Model):
    __tablename__ = 'channel'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    alias = db.Column(db.String)
    articles_count = db.Column(db.Integer)
    parent_id = db.Column(db.Integer, db.ForeignKey('channel.id'))
    articles = db.relationship('Article', backref='catalog')
    catalogs = db.relationship('Channel', backref=db.backref('parent', remote_side=[id]))

class Tag(db.Model):
    __tablename__ = 'tag'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    alias = db.Column(db.String)
    articles_count = db.Column(db.Integer)
首先,我想获取所有的Artciel,因此我编写如下代码:

articles = self.query.options(joinedload('catalog'),joinedload('tags')).all()
它响应sql脚本:

SELECT 
    article.id AS article_id, 
    article.title AS article_title, 
    article.content AS article_content, 
    article.cover_image AS article_cover_image, 
    article.url_title AS article_url_title, 
    article.seo_keyword AS article_seo_keyword, 
    article.seo_description AS article_seo_description, 
    article.author AS article_author, 
    article.created_date AS article_created_date, 
    article.views_count AS article_views_count, 
    article.comments_count AS article_comments_count,
    article.catalog_id AS article_catalog_id, 
    tag_1.id AS tag_1_id, 
    tag_1.name AS tag_1_name, 
    tag_1.alias AS tag_1_alias, 
    tag_1.articles_count AS tag_1_articles_count, 
    channel_1.id AS channel_1_id, 
    channel_1.name AS channel_1_name, 
    channel_1.alias AS channel_1_alias, 
    channel_1.articles_count AS channel_1_articles_count, 
    channel_1.parent_id AS channel_1_parent_id 
FROM 
    article 
LEFT OUTER JOIN 
    tag_article_table AS tag_article_table_1 ON article.id = tag_article_table_1.article_id 
LEFT OUTER JOIN 
    tag AS tag_1 ON tag_1.id = tag_article_table_1.tag_id 
LEFT OUTER JOIN 
    channel AS channel_1 ON channel_1.id = article.catalog_id
但当我尝试向代码添加偏移量和限制时,如:

articles = self.query.options(joinedload('catalog'),joinedload('tags')).offset(10).limit(10).all()
sql脚本将变成:

SELECT 
    anon_1.article_id AS anon_1_article_id, 
    anon_1.article_title AS anon_1_article_title, 
    anon_1.article_content AS anon_1_article_content, 
    anon_1.article_cover_image AS anon_1_article_cover_image, 
    anon_1.article_url_title AS anon_1_article_url_title, 
    anon_1.article_seo_keyword AS anon_1_article_seo_keyword, 
    anon_1.article_seo_description AS anon_1_article_seo_description, 
    anon_1.article_author AS anon_1_article_author, 
    anon_1.article_created_date AS anon_1_article_created_date, 
    anon_1.article_views_count AS anon_1_article_views_count, 
    anon_1.article_comments_count AS anon_1_article_comments_count,
    anon_1.article_catalog_id AS anon_1_article_catalog_id, 
    tag_1.id AS tag_1_id, 
    tag_1.name AS tag_1_name, 
    tag_1.alias AS tag_1_alias, 
    tag_1.articles_count AS tag_1_articles_count, 
    channel_1.id AS channel_1_id, 
    channel_1.name AS channel_1_name, 
    channel_1.alias AS channel_1_alias, 
    channel_1.articles_count AS channel_1_articles_count, 
    channel_1.parent_id AS channel_1_parent_id 
FROM (
    SELECT 
        article.id AS article_id, 
        article.title AS article_title, 
        article.content AS article_content, 
        article.cover_image AS article_cover_image, 
        article.url_title AS article_url_title, 
        article.seo_keyword AS article_seo_keyword, 
        article.seo_description AS article_seo_description, 
        article.author AS article_author, 
        article.created_date AS article_created_date, 
        article.views_count AS article_views_count, 
        article.comments_count AS article_comments_count, 
        article.catalog_id AS article_catalog_id 
    FROM article
    LIMIT 
        %(param_1)s OFFSET %(param_2)s) AS anon_1 
    LEFT OUTER JOIN 
        tag_article_table AS tag_article_table_1 ON anon_1.article_id = tag_article_table_1.article_id 
    LEFT OUTER JOIN 
        tag AS tag_1 ON tag_1.id = tag_article_table_1.tag_id 
    LEFT OUTER JOIN 
        channel AS channel_1 ON channel_1.id = anon_1.article_catalog_id 
)
其中还有一个子查询,除了添加限制和偏移量,我什么也没做


我对此感到困惑。这是sqlalchemy错误还是错误?

我猜实现取决于您使用的
RDBMS
,因为并非所有实现都是
限制/偏移量
,需要采取一些变通措施。不管怎样,你试过
articles=self.query.options(joinedload('catalog')、joinedload('tags'))[10:20]
吗?好的,它确实有效。我使用[10:20],子查询就不会再出现了!好极了好奇:你使用哪种数据库管理系统?postgresql。RDBMS和这个错误有什么关系吗?我不知道,对不起。