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

[Python] How to set an Array column with an empty array as default in SQLAlchemy + Postgres

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 17:02.

  1. Stack

    Stack Membro Participativo

    I want to list the sources of a piece of information. Instead of creating another table with a one to many relation, I tought I'd use the Array type.

    To I tried:

    app = Flask(__name__)
    db = SQLAlchemy(app)

    ...

    class Edge(db.Model):

    sources = db.Column(
    db.ARRAY(db.String),
    default=db.ARRAY(db.String)
    )


    But adding an edge gives me this error:


    ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'ARRAY' [SQL: 'INSERT INTO edges (child_id, parent_id, sources) VALUES (%(child_id)s, %(parent_id)s, %(sources)s'] [parameters: {'child_id': 20, 'parent_id': 26, 'sources': ARRAY(String())}]

    I can't find a good tutorial on how to use an array column with a default empty array.

    Thanks

    Continue reading...

Compartilhe esta Página