Sangram Badi
Sangram Badi

Reputation: 4264

How to fetch varchar type data from MySQL using flask_sqlalchemy?

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?

Upvotes: 0

Views: 298

Answers (1)

Waket Zheng
Waket Zheng

Reputation: 6331

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)

Upvotes: 1

Related Questions