为什么我在 SQLAlchemy 中的连接查询无法使用 4 个过滤器

问题描述 投票:0回答:1

我正在构建一个生日提醒应用程序,并正在开发烧瓶中的通知铃声。当我打开通知时,它应该填充用户关注的用户生日,这些生日将在不到一个月和/或今天出现。即使数据库中有明确的生日在正确的日期和时间范围内,也不起作用。

@app.context_processor
def base():
    def notifbday():
        if current_user.is_authenticated:
            user_id = flask_login.current_user.id
            # Get the current date
            today = datetime.now()
            # Calculate the date one month earlier
            one_month_later = today + relativedelta(months=1)
            print(one_month_later)

            currentyear = func.extract('year', today)

            birthdays_in_last_month = User.query.join(
                Follow, User.id == Follow.follower_id
            ).filter(
                    func.extract('month', User.birthday) <= one_month_later.month,
                    func.extract('day', User.birthday) <= one_month_later.day,
                    func.extract('month', User.birthday) >= today.month,
                    func.extract('day', User.birthday) >= today.day
            ).all()

            print(birthdays_in_last_month)
            
            return birthdays_in_last_month

    return dict(notif=notifbday)

当我只这样做时它确实有效,但在添加所有四个过滤器后停止了。

birthdays_in_last_month = User.query.join(
                Follow, User.id == Follow.follower_id
            ).filter(
                    func.extract('month', User.birthday) >= today.month,
                    func.extract('day', User.birthday) >= today.day
            ).all()
<div class="popup" id="myPopup">
        <i id="notif" class="fa fa-bell" style="font-size: 48px; color: red"></i>
        <div id="popuptext">
          {% if notif() != None %} {% for n in notif() %}
          <div class="notifmsgs">
            <p>
              {{ n.username }}'s birthday is coming up on {{ n.birthday.strftime('%Y-%m-%d') }}
            </p>

          </div>
          {% endfor %}{% endif %}

        </div>
      </div>
python database flask flask-sqlalchemy
1个回答
0
投票

用户查询连接已反转。必须改为

User.id == Follow.followed_id
而不是
follower_id

@app.context_processor
def base():
    def notifbday():
        print('test')
        if current_user.is_authenticated:
            print('test')
            user_id = flask_login.current_user.id
            # Get the current date
            today = datetime.now()
            # Calculate the date one month earlier
            one_month_later = today + relativedelta(months=1)
            print(one_month_later)
            one_month_before = today - relativedelta(months=12)

            birthdays_in_last_month = User.query.join(
                Follow, User.id == Follow.followed_id
            ).filter(
                (func.extract('month', User.birthday) >= today.month),
            (func.extract('month', User.birthday) <= one_month_later.month)).all()

            print(birthdays_in_last_month)
            
            return birthdays_in_last_month
        else:
            print('user is not logged in')

    return dict(notif=notifbday)
© www.soinside.com 2019 - 2024. All rights reserved.