OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter ted
  • Start date Start date
T

ted

Guest
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:

Code:
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
<p>I want to list the <code>sources</code> of a piece of information. Instead of creating another table with a <code>one to many</code> relation, I tought I'd use the <code>Array</code> type.</p>
<p>To I tried:</p>
<pre><code>app = Flask(__name__)
db = SQLAlchemy(app)

...

class Edge(db.Model):

sources = db.Column(
db.ARRAY(db.String),
default=db.ARRAY(db.String)
)
</code></pre>
<p>But adding an <code>edge</code> gives me this error:</p>
<blockquote>
<p>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())}]</p>
</blockquote>
<p>I can't find a good tutorial on how to use an <code>array</code> column with a default empty array.</p>
<p>Thanks</p>
 

Latest posts

Top