I have a script for injecting data into my sqlalchemy database and it fails to save an association for unknown reasons. It doesn't give an error or bug it just doesn't save anything. It does the same thing in the console.
from api import create_app, models, db
app = create_app()
app.app_context().push()
Type = models.Type
TypeSettings = models.TypeSettings
ts1 = TypeSettings(isHome=True, layoutType='classic', productCard='neon')
t1 = Type(name='Grocery', slug='grocery', icon='FruitsVegetable', settings=ts1)
db.session.add(t1, ts1)
db.session.commit()
Here is a console from Flask Shell showing it does not save the association.
>>> db.session.scalars(Language.select()).all()[0]
<api.models.Language object at 0x0000020D4213E650>
>>> db.session.scalars(Language.select()).all()[0].language
'en'
>>> t1=db.session.scalars(Type.select()).all()[0]
>>> ts1=db.session.scalars(TypeSettings.select()).all()[0]
>>> t1.settings #shows null which makes sense because it hasn't been set
>>> t1.settings=ts1
>>> t1.settings #appears to have saved properly but not yet committed
<api.models.TypeSettings object at 0x0000020D421DB460>
>>> db.session.commit()
>>> t1.settings #shows null after commit...
I am using Flask-SQLAlchemy==2.5.1 and SQLAlchemy==1.4.32
Here is my shell context so you know how it is built
# define the shell context
@app.shell_context_processor
def shell_context(): # pragma: no cover
ctx = {'db': db}
for attr in dir(models):
model = getattr(models, attr)
if hasattr(model, '__bases__') and \
db.Model in getattr(model, '__bases__'):
ctx[attr] = model
return ctx