0

In manage.py:

import os
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand

from app import app, db
from app.models import *


app.config.from_object(os.environ['APP_SETTINGS'])

migrate = Migrate(app, db)
manager = Manager(app)

manager.add_command('db', MigrateCommand)
manager.add_command('shell', Shell(use_ipython=True))


if __name__ == '__main__':
    manager.run()

I have a model with:

...
class Edge(db.Model):
    __tablename__ = 'edges'

    sources = db.Column(
        db.ARRAY(db.String),
        server_default="{}"
    )
    ...

Then I run python manage.py shell and within the shell

e = Edge.query.first()
assert len(e.sources) == 0
e.sources.append('hello')
db.session.add(e)
db.session.commit()

But then closing the shell and going back in, this very edge still has no source (e.sources is [])


Also tried

db.session.object_session(e).add(e)
db.session.object_session(e).commit()

I noticed that after db.session.commit(), e.sources is [] again!

e = Edge.query.first()
assert len(e.sources) == 0
e.sources.append('hello')
e.sources
>>> ['hello']
db.session.add(e)
db.session.commit()
e.sources
>>>[]

Can it have anything to do with the server_default in the Column definition?

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
ted
  • 13,596
  • 9
  • 65
  • 107

0 Answers0