Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError when iterating over query from subquery with CTE #2320

Closed
Neirop opened this issue Jan 3, 2021 · 1 comment
Closed

KeyError when iterating over query from subquery with CTE #2320

Neirop opened this issue Jan 3, 2021 · 1 comment

Comments

@Neirop
Copy link

Neirop commented Jan 3, 2021

Database: Postgresql 12
Peewee version: 3.14.0

Model:

class BaseModel(Model):
    class Meta:
        database = database


class Channel(BaseModel):
    channel_id = IntegerField(primary_key=True)
    channel_name = TextField()

    class Meta:
        table_name = 'channel'


class UserMessage(BaseModel):
    message_id = UUIDField(index=True, primary_key=True)
    channel_id = ForeignKeyField(column_name='channel_id', field='channel_id', model=Channel, on_delete='CASCADE',
                                 lazy_load=False, index=False)
    user_id = IntegerField()
    timestamp = IntegerField()

    class Meta:
        table_name = 'user_message'
        indexes = (
            (('channel_id', 'timestamp'), False),
        )

Query:

UserMessageAlias = UserMessage.alias()
    subquery = UserMessageAlias \
        .select(UserMessageAlias, fn.RANK().over(order_by=[UserMessageAlias.timestamp.desc()])
                .alias("rank")) \
        .where(UserMessageAlias.channel_id == 26) \
        .order_by(UserMessageAlias.timestamp.desc()) \
        .limit(200)

    users = set()
    for i in range(100):
        users.add((random.randint(0, 999), random.randint(0, 150)))

    user_cte = ValuesList(users).cte("user_streamer", columns=["uid", "limit"])
    query = UserMessage \
        .select(subquery.c.message_id, subquery.c.channel_id, subquery.c.user_id,
                subquery.c.timestamp, subquery.c.rank) \
        .from_(subquery) \
        .join(user_cte, on=(
            (subquery.c.user_id == user_cte.c.uid) &
            (subquery.c.rank <= user_cte.c.limit))) \
        .with_cte(user_cte)

    for mes in query:
        print(mes)

I got this error:

Traceback (most recent call last):
  File "test.py", line 111, in <module>
    get_messages()
  File "test.py", line 100, in test_bug
    for mes in query:
  File "/tmp/test/venv/lib/python3.8/site-packages/peewee.py", line 4361, in next
    self.cursor_wrapper.iterate()
  File "/tmp/test/venv/lib/python3.8/site-packages/peewee.py", line 4280, in iterate
    result = self.process_row(row)
  File "/tmp/test/venv/lib/python3.8/site-packages/peewee.py", line 7548, in process_row
    instance = objects[key]
KeyError: <peewee.ModelSelect object at 0x7fbee0c78d90>

But if I use dicts() or tuples(), it works

@coleifer
Copy link
Owner

Thanks for the succinct report. This is now resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants