You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When testing the DB connections with SQLA I realised that there was a fail mode. If one imports the model classes for a schema and the database connection has failed to connect (for example because the domain of the profile is incorrect) then the import will fail because calling Base.prepare(database.engine) fails because there is no engine.
To fix that I bound the base to the database connection and added a prepare_base method that gets called when the database connects, and then it calls Base.prepare. In the model classes, instead of doing Base.prepare one does database.add_base to bind the base.
I also found that one cannot have the relations defined at the end of the file if the database is not connected. It will fail because the column names have not been created. I added a _relations parameter to the base that allows to define a function (I called it define_relations) that gets called bat database.prepare_bases. This allows the full model classes to be imported even if there is not a database connected and then prepare the classes and create the relations during connection.
It's a hack and I'm not super happy about it but it works. There could be a better way to do this, though, so we may want to have a look. Since this is all behind-the-scenes stuff we can change it without the user noticing it.
At the very least we should add some tests to make sure there are no more weird corner cases.
The text was updated successfully, but these errors were encountered:
When testing the DB connections with SQLA I realised that there was a fail mode. If one imports the model classes for a schema and the database connection has failed to connect (for example because the domain of the profile is incorrect) then the import will fail because calling
Base.prepare(database.engine)
fails because there is no engine.To fix that I bound the base to the database connection and added a
prepare_base
method that gets called when the database connects, and then it callsBase.prepare
. In the model classes, instead of doingBase.prepare
one doesdatabase.add_base
to bind the base.I also found that one cannot have the relations defined at the end of the file if the database is not connected. It will fail because the column names have not been created. I added a
_relations
parameter to the base that allows to define a function (I called itdefine_relations
) that gets called batdatabase.prepare_bases
. This allows the full model classes to be imported even if there is not a database connected and then prepare the classes and create the relations during connection.It's a hack and I'm not super happy about it but it works. There could be a better way to do this, though, so we may want to have a look. Since this is all behind-the-scenes stuff we can change it without the user noticing it.
At the very least we should add some tests to make sure there are no more weird corner cases.
The text was updated successfully, but these errors were encountered: