I created a following table in postgresql
.
class Test(db.Model):
__tablename__ = 'Test'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
genres = db.Column(db.ARRAY(db.String(120)))
To save genres
into the Test Table, I read values from the UI and store it in the DB through the following code,
genres = request.form.getlist('genres')
test = Test(name=name, genres=genres)
db.session.add(test)
db.session.commit()
and while reading it from the DB, I do,
test = Test.query.get(test_id)
data = test.serialize()
print(f'Data => {data}')
The problem I am running into is When I read genres
from Test table it is returned as an array of characters,
'genres': ['{', 'A', 'l', 't', 'e', 'r', 'n', 'a', 't', 'i', 'v', 'e', ',', 'B', 'l', 'u', 'e', 's', ',', 'C', 'l', 'a', 's', 's', 'i', 'c', 'a', 'l', '}']
rather than the way they were created/stored & saved into the table, which is an array of Strings,
genres = ['Alternative', 'Blues', 'Classical']
Edit: Even if I write the result before Serialization
happens I still see this issue. Here is resulting genres
upon running the `test.query.get('id'),
genres: ['{', 'A', 'l', 't', 'e', 'r', 'n', 'a', 't', 'i', 'v', 'e', ',', 'B', 'l', 'u', 'e', 's', ',', 'C', 'l', 'a', 's', 's', 'i', 'c', 'a', 'l', '}']
What am I doing wrong?
db.String()
todb.ARRAY(db.String())
without migrating the database. The flask-migrate tool didn't detect any changes so I created the tables from scratch and used this line in the alembic script to create the column:sa.Column('name', sa.ARRAY(sa.String()), nullable=True)
.__tablename__ = "table_name"