I have many types of authentications, therefore I have one base postgres table (called "authentications"), and other types of authentications are derived from it. For example, an entity that gets authenticated using a password is called "password_authentications" and it inherits from the "authentications" table using postgres. The schema is thus:

CREATE TABLE authentications (
 id 
)

CREATE TABLE password_authentications (
 id
 password
) inherits (authentications)

This is very elegant, because I also have a table called "Users" that has a foreignkey into the authentications table:

CREATE TABLE users (
 id
 username
 authentications_id
)

Hence a user can be authenticated by anything that inherits from authentications. So in future, if we need to authenticate users using RSA (for example), all I would do is make a base class from the authentications that described RSA authentication.

Now I want to use SQLAlchemy to build a mapping between Python classes and this structure. Specifically, I want to map a class called "User" to the table called "users" (that's easily done) but I want a relationship on password_authentication (not on authentication). In fact, I am looking to build this relationship dynamically, so if one user is authenticated using password, then SQLAlchemy will link the "Users" table to "Password_Authentications" table. If there is another type of auhentication, then User should be linked to that table instead. The only thing that all tables that User will link to have in common is that they are derived from Authentications table. Hence the "Users" table has a foreignkey into this table.

How can I accomplish the above? Is it even possible?

share|improve this question

43% accept rate
feedback

1 Answer

SA does not support the INHERITS of postgresql in terms of model object inheritance. However, please take a look at PostgreSQLInheritance recipe, where Mike shows how to use the two together and the limitations thereof.

Also refer to the answer to the Python, SQLAlchemy and Postgresql: understanding inheritance, where TokenMacGuy explains the difference between the Inheritance in terms of OOP and postgresql table inheritance.


Posting code samples from the linked resources would result in unnecessary duplication and rather lengthy answer, which still would not be able to provide you with the complete solution.

share|improve this answer
But each postgres table has a tableoid column, which can very easily be used as a discriminator for concrete inheritance. So I can't understand why this will not work! – Barry Steyn Jun 28 at 23:18
Why can't I use concrete table inheritance with the tableoid as a discriminator??? – Barry Steyn Jun 29 at 2:06
I believe you can make it work, but it would not be as nice and portable as you might think: 1) you have no control over tableoids; 2) they are numeric and not so readable; 3) also you will have to hard-code their values in your Python model for each polymorphic_identity value; 4) as soon as you migrate from test to production, the tableoids might be different again, so you will have to change your codebase to make it work;... I am sure there are other caveats, which I cannot even think of as I do not use postresql. – van Jun 29 at 9:07
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.