Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLA relations when DB not auto connected #6

Open
albireox opened this issue Dec 11, 2018 · 0 comments
Open

SQLA relations when DB not auto connected #6

albireox opened this issue Dec 11, 2018 · 0 comments
Assignees

Comments

@albireox
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants