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

Extra `t2` while generating query #983

Closed
blueset opened this Issue Jun 14, 2016 · 5 comments

Comments

Projects
None yet
3 participants
@blueset

blueset commented Jun 14, 2016

Model

class MsgLog(Model):
    master_msg_id = CharField(unique=True, primary_key=True)
    text = CharField()
    slave_origin_uid = CharField()
    slave_origin_display_name = CharField(null=True)
    slave_member_uid = CharField(null=True)
    slave_member_display_name = CharField(null=True)
    msg_type = CharField()
    sent_to = CharField()

    class Meta:
        database = SqliteDatabase('data.db')

Query

chat_id = 1
q = MsgLog.select().where(MsgLog.master_msg_id.startswith("%s." % chat_id)).order_by(MsgLog.id.desc())
print(q)

Expected result

SELECT "t1"."master_msg_id", "t1"."text", "t1"."slave_origin_uid", "t1"."slave_origin_display_name", "t1"."slave_member_uid", "t1"."slave_member_display_name", "t1"."msg_type", "t1"."sent_to" FROM "msglog" AS t1 WHERE ("t1"."master_msg_id" LIKE ?) ORDER BY "t1"."id" DESC ['1.%']

Returned result

SELECT "t1"."master_msg_id", "t1"."text", "t1"."slave_origin_uid", "t1"."slave_origin_display_name", "t1"."slave_member_uid", "t1"."slave_member_display_name", "t1"."msg_type", "t1"."sent_to" FROM "msglog" AS t1 WHERE ("t1"."master_msg_id" LIKE ?) ORDER BY "t2"."id" DESC ['1.%']
@coleifer

This comment has been minimized.

Owner

coleifer commented Jun 14, 2016

q = MsgLog.select().where(MsgLog.master_msg_id.startswith("%s." % chat_id)).order_by(MsgLog.id.desc())

The problem is you're ordering by MsgLog.id but MsgLog.master_msg_id is the primary key and the model (while it has an "id" attribute), does not consider it a valid field.

To fix this, just change your "order_by()" call to order on a field that's defined on the MsgLog model. Or alternatively, you could make "master_msg_id" have "unique=True" instead of being a primary key.

@coleifer coleifer closed this Jun 14, 2016

@coleifer

This comment has been minimized.

Owner

coleifer commented Jun 14, 2016

I think I may make some modifications to Peewee to drop the id attribute if a different field is used as the foreign key.

@keredson

This comment has been minimized.

Contributor

keredson commented Jun 14, 2016

@coleifer deleting other user's comments just because you don't agree with the technical viewpoint is very uncool.

@coleifer

This comment has been minimized.

Owner

coleifer commented Jun 14, 2016

The truth is that I removed your comments because they were not constructive, and I was tired of having to defend aspects of my project because you felt entitled to a technical discussion because you happened to disagree with it. When I pointed to you that the things you disagreed with were documented and obviously intentional (via git log), you suggested I was trying to justify, post-hoc, bad design decisions, and was being an irresponsible maintainer. How is that constructive?

@coleifer

This comment has been minimized.

Owner

coleifer commented Jun 15, 2016

Reopening as I intend to fix.

@coleifer coleifer reopened this Jun 15, 2016

@coleifer coleifer closed this in 6b7c013 Jul 14, 2016

coleifer pushed a commit that referenced this issue Jul 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment