1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to query a SQL View from flask-SQLAlchemy

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024 às 00:12.

  1. Stack

    Stack Membro Participativo

    I have been given access to a database (MySQL, in case it matters) to access some data that I need, but I don't have direct access to the tables, the data is exposed through a SQL View.

    I want to wrap this in a small API using flask. For the database connection I intend to use SQLAlchemy (just because it's the only databse wrapper that I know in python).

    The thing is, I have read a lot of documentation, and dozens of stackoverflow posts and guides through the Internet, and I can't seem to find a way to query SQL Views from flask-SQLAlchemy.

    In the documentation, they always use Models. I tried to define my View as a Model, but I get could not assemble any primary key columns for mapped table 'MySQLView'.

    What I tried to do:

    class MyView(db.Model):
    __tablename__ = "my_view_name_in_sql"
    db.Column("id", db.Integer, primary_key=True)
    db.Column("sample_id", db.String)
    {...etc}


    Is there a way to do this, or maybe I should opt-out of using flask-SQLAlchemy and use something else instead?

    Continue reading...

Compartilhe esta Página