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
I eventually found the answer in the comments here:
sources = db.Column(
db.ARRAY(db.String),
server_default="{}"
)
A python callable can also be set as default value:
sources = db.Column(
db.ARRAY(db.String),
default=dict
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With