0

I am have a flask application which is interaction with MySQL db using flask_sqlalchemy package. I am trying to fetch data where slug = 'my_slug_data' and is_published=false and is_delete=false using below query.but i am not getting any response back.

aPost = Posts.query.filter_by(slug=slug, is_published=True, is_delete=False)
print(aPost)

When i am printing aPost. then it is showing below query

SELECT posts.id AS posts_id, posts.created_by AS posts_created_by, posts.created_on AS posts_created_on, posts.updated_on AS posts_updated_on, posts.is_published AS posts_is_published, posts.is_delete AS posts_is_delete, posts.title AS posts_title, posts.slug AS posts_slug, posts.sub_title AS posts_sub_title, posts.content AS posts_content, posts.tags AS posts_tags
FROM posts
WHERE posts.slug = %(slug_1)s AND posts.is_published = true AND posts.is_delete = false

Below is posts model

class Posts(db.Model):
    #id, created_by, created_on, updated_on, is_published, is_delete, title, slug, sub_title, content, tags

    id = db.Column(db.Integer, primary_key=True)
    created_by = db.Column(db.Integer, nullable=False)
    created_on = db.Column(db.String(12), nullable=True)
    updated_on = db.Column(db.String(12), nullable=True)
    is_published = db.Column(db.Boolean, nullable=True)
    is_delete = db.Column(db.Boolean, nullable=True)
    title = db.Column(db.String(100), nullable=False)
    slug = db.Column(db.String(130), nullable=True, unique=True)
    sub_title = db.Column(db.String(150), nullable=True)
    content = db.Column(db.String(), nullable=False)
    tags = db.Column(db.JSON, nullable=True)

Can anyone guide me where i am doing wrong in the query?

4
  • You should paste your Posts model Commented May 5, 2020 at 6:36
  • @WaketZheng updated Commented May 5, 2020 at 6:39
  • @hd1 do you mean, i need to rename slug to some other name ? Commented May 5, 2020 at 6:49
  • Apologies for the misleading comment, @SangramBadi Commented May 5, 2020 at 7:44

1 Answer 1

1

Try this:

for obj in Posts.query.all():
    print(obj.id, obj.slug, obj.is_published, obj.is_delete)
print(Posts.query.filter_by(slug=slug))
print(Posts.query.filter_by(slug=slug, is_published=True))
aPost = Posts.query.filter_by(slug=slug, is_published=True, is_delete=False).first()
print(aPost)
Sign up to request clarification or add additional context in comments.

5 Comments

i am getting error (<class 'TypeError'>, TypeError('count() takes exactly one argument (0 given)'), <traceback object at 0x00000186967692C0>) for print(Posts.query.all().count())
Sorry, I use Django now, and does not write code about flask_sqlalchemy for a long time. But it is better to check whether there is data in db.
print(Posts.query.all()) - i am getting output now. There are 2 records and i am getting
I got the answer need to use aPost = Posts.query.filter_by(slug=slug, is_published=True, is_delete=False).first()
As i got the clue from your answer, i will accept your answer. but if you modify your answer with .first()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.