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

Genarated query problem #913

Closed
ck196 opened this issue Apr 19, 2016 · 3 comments
Closed

Genarated query problem #913

ck196 opened this issue Apr 19, 2016 · 3 comments

Comments

@ck196
Copy link

ck196 commented Apr 19, 2016

I do deleted query on Relationship table

query = Relationship.delete().where(Relationship.video == video)

<class 'libs.basemodel.Relationship'> DELETE FROM "relationship" WHERE ("video_id" = (SELECT "t2"."id" FROM "video" AS t1 WHERE ("t1"."name" = ?) ORDER BY "t1"."add_date" DESC)) [u'v5.mp4']

Problem:
OperationalError: no such column: t2.id

My models very simple:

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

class User(BaseModel):
    username` = CharField(unique=True)
    password = CharField()
    email = CharField()
    join_date = DateTimeField()

    class Meta:
        order_by = ('username',)

class Relationship(BaseModel):
    video = ForeignKeyField(User, related_name='relationships')
    to_user = ForeignKeyField(User, related_name='related_to')
    class Meta:
        indexes = (
            # Specify a unique multi-column index on from/to-user.
            (('video', 'to_user'), True),
        )

class Video(BaseModel):
    name = CharField(unique=True)
    width = IntegerField()
    height = IntegerField()
    step = IntegerField()
    frame_count = IntegerField()
    add_date = DateTimeField()

    class Meta:
        order_by = ('-add_date',)
@coleifer
Copy link
Owner

Please fix the formatting of code.

@ck196
Copy link
Author

ck196 commented Apr 21, 2016

Sorry, i've updated code formatting.

@coleifer
Copy link
Owner

coleifer commented Apr 23, 2016

To fix this I believe if you explicitly select the Video ID column you'll be set:

video = Video.select(Video.id).where(Video.name == 'v5.mp4')
query = Relationship.delete().where(Relationship.video == video)
print query

Outputs:

DELETE FROM "relationship" 
WHERE ("video_id" = (
  SELECT "t1"."id" 
  FROM "video" AS t1 
  WHERE ("t1"."name" = ?) 
  ORDER BY "t1"."add_date" DESC
))

If you've already got the query from somewhere else and want to modify the SELECT clause, you can do:

query = Relationship.delete().where(Relationship.video == video.select(Video.id))

An unrelated suggestion:

Never ever ever specify a default ordering for your models. It's a code smell and I regret building it into Peewee's API (the idea comes from Django originally, and the Django community seems to feel the same way about it). If you want a default ordering that's easily accessible, just do something like:

class Video(BaseModel):
    # ... fields ...

    @classmethod
    def all(cls):
        return cls.select().order_by(cls.add_date.desc())

# ------ elsewhere in your code ------ #

videos = Video.all()

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