在sqlalchemy中混淆了SQL LIMIT和OFFSET

时间:2013-12-30 05:01:38

标签: python sqlalchemy

大家。我在使用FLask和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):
    __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)

首先我想要获取所有的artciels,所以我写这样的代码:

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

但是当我尝试将OFFSET和LIMIT添加到代码中时:

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 
)

其中有一个额外的子查询,除了添加LIMIT和OFFSET之外我什么都没做。

我对它感到困惑。这是一个sqlalchemy bug还是错误?

0 个答案:

没有答案
相关问题