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

UserInfo can't have a ForeignKey with JupyterHub User model #26

Closed
leportella opened this issue Jan 9, 2019 · 6 comments
Closed

UserInfo can't have a ForeignKey with JupyterHub User model #26

leportella opened this issue Jan 9, 2019 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@leportella
Copy link
Collaborator

leportella commented Jan 9, 2019

So, I created a class called UserInfo to store extra information on the user that the default JupyterHub User class won't. The UserInfo class can be seen here.

This table (user_info) is created on the Authenticator __init__ method by the following way:
UserInfo.__table__.create(self.db) where self.db is a SQLAlchemy Engine instance.

However, when I try to add a relationship between UserInfo and User, this method won't work. Usually I get this error: NoReferencedTableError: Foreign key associated with column 'product.user_id' could not find table 'user' with which to generate a foreign key to target column 'id'

I created a simple example and the main problem that I found is that, if I create both tables at the same time (using Base.metadata.create_all(engine)) everything works fine. The problem is creating the relationship after the User table has already been created.

Since I am not changing anything on JupyterHub, the User table is always created before the moment I create the UserInfo table, so I can't change this for now.

Some things I tried on the process:

  • I tried to add the relationship to the User class before the creation of the new table by using this command: User.__mapper__.add_property('products', relationship("product", collection_class=set))
  • I tried using a automap_base instead of a declarative_base, then created the UserInfo table before by using self.db.execute(..SQL..) and then get the table using automap.

For now, my UserInfo is not related to User, and it only uses the same username in the momento of creation since I have no idea how to fix this.

@leportella leportella added the help wanted Extra attention is needed label Jan 9, 2019
@yuvipanda
Copy link
Contributor

@minrk heya! Do you think you'll have any time to help me / @leportella out on this one? We're trying to use the same db that JupyterHub is using for the authenticator too...

@dedeco
Copy link

dedeco commented Jan 10, 2019

The tables User and UserInfo are in the same schema (in database)? Using the Base = declarative_base() the objects should share the same variable Base... Can you import and use the same variable Base of jupyterhub/jupyterhub/orm.py?

@leportella
Copy link
Collaborator Author

@dedeco I just tested it on a simple example I was working and it seems to work fine! I'll test using User and UserInfo and let you guys know! Thank you so much!

@leportella
Copy link
Collaborator Author

@dedeco , @yuvipanda it worked!

@minrk
Copy link
Member

minrk commented Jan 11, 2019

Sorry, didn't catch up before it looks like it was solved. Great!

I think I would probably advise strongly against custom authenticators using the private jupyterhub.orm and creating anything in the jupyterhub databases directly, though. There's absolutely no guarantee that anything there is stable for public consumption (not the tables, not the classes, not the orm module existing at all).

@leportella
Copy link
Collaborator Author

@minrk I'll open another issue to discuss this, since this is closed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants