[Python] getting unexpected HTTP status 307 but the query still executes

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    INFO: 127.0.0.1:54497 - "GET /posts HTTP/1.1" 307 Temporary Redirect
    2025-03-02 18:55:15,531 INFO sqlalchemy.engine.Engine select pg_catalog.version()
    2025-03-02 18:55:15,532 INFO sqlalchemy.engine.Engine [raw sql] {}
    2025-03-02 18:55:15,534 INFO sqlalchemy.engine.Engine select current_schema()
    2025-03-02 18:55:15,534 INFO sqlalchemy.engine.Engine [raw sql] {}
    2025-03-02 18:55:15,536 INFO sqlalchemy.engine.Engine show standard_conforming_strings
    2025-03-02 18:55:15,536 INFO sqlalchemy.engine.Engine [raw sql] {}
    2025-03-02 18:55:15,538 INFO sqlalchemy.engine.Engine BEGIN (implicit)
    2025-03-02 18:55:15,542 INFO sqlalchemy.engine.Engine SELECT posts.post_id AS posts_post_id, posts.title AS posts_title, posts.content AS posts_content, posts.published AS posts_published, posts.created_at AS posts_created_at
    FROM posts
    2025-03-02 18:55:15,543 INFO sqlalchemy.engine.Engine [generated in 0.00138s] {}
    2025-03-02 18:55:15,552 INFO sqlalchemy.engine.Engine ROLLBACK


    i am learning of using fastapi in python and sqlalchemy. in this case, the code that executes is simple just this:

    @router.get("/")
    def ger_posts(db: Session = Depends(get_session)):
    posts = db.query(Posts).all()
    return posts


    and db looks like this:

    DB_URL = "postgresql://postgres:pass@localhost/FastAPI"

    engine = create_engine(DB_URL, echo=True)

    Base = declarative_base()

    class Posts(Base):
    __tablename__ = "posts"

    post_id = Column(Integer, primary_key=True, index=True, nullable=False)
    title = Column(String, nullable=False)
    content = Column(String, nullable=False)
    published = Column(Boolean, nullable=True, server_default="TRUE")
    created_at = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False)

    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

    def get_session():
    session = SessionLocal()
    try:
    yield session
    finally:
    session.close()


    I tried to use AI but they actually could not help me.

    Continue reading...

Compartilhe esta Página