[Python] Flask-sqlalchemy session is not fetching data when creating under a app.cli.command()

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    Base.metadata.bind = engineDBSession = sessionmaker(bind=engine)

    @app.cli.command("generate_task_notifications")
    def createTaskNotification(): # Run scheduled job for creating task
    statuses.print('starting jobs')
    generate_task_notifications()
    print('scheduled jobs executed at '+ str(datetime.now()))
    return

    def generate_task_notifications():
    session = DBSession()
    upcoming_days_upto = int(session.query(SystemConfig).filter(SystemConfig.Key == e_SysConfig.UpcomingDay.Key).first().Value)
    session.close()
    return upcoming_days_upto


    If I run the same as an API endpoint then it's working fine but when I am trying to use the same as Flask command then upcoming_days_upto is giving error AttributeError: 'NoneType' object has no attribute 'Value'.

    Though data is there in the database, I have checked the same using Beekeeper Studio as I am accommodating the SQLite database.

    I tried shifting the session creation code inside the CLI command function but I was unlucky and when try to run the same as an API endpoint then it works fine.

    Continue reading...

Compartilhe esta Página