fromSelect
fromSelect

Reputation: 23

How to filter by date.attribute in gino with Postgres

I try to filter users by month using gino

cur_month_users = await User.query.where(User.birth_date.month==12).gino.all()

but it does't work cus:

AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'month'

my simple model with date column

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer(), primary_key=True)
    discord_id = db.Column(db.Integer())
    user_name = db.Column(db.Unicode())
    birth_date = db.Column(db.Date())

    @property
    def month_and_day(self):
        return self.birth_date.strftime(format="%d.%m")

    def __str__(self):
        return self.user_name

How should i solve my issue?

Upvotes: 0

Views: 292

Answers (1)

jorzel
jorzel

Reputation: 1346

I don't know gino, but in standard sqlalchemy (and sql) you cannot get month from date in query directly. You should use function extracting month from date, like date_part:

from sqlalchemy import func
cur_month_users = await User.query.where(
    func.date_part('month', User.birth_date) == 12
).gino.all()

Upvotes: 0

Related Questions