Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upUserInfo can't have a ForeignKey with JupyterHub User model #26
Comments
leportella
added
the
help wanted
label
Jan 9, 2019
This comment has been minimized.
This comment has been minimized.
@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... |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
@dedeco I just tested it on a simple example I was working and it seems to work fine! I'll test using |
This comment has been minimized.
This comment has been minimized.
@dedeco , @yuvipanda it worked! |
leportella
closed this
in
#27
Jan 11, 2019
This comment has been minimized.
This comment has been minimized.
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). |
This comment has been minimized.
This comment has been minimized.
@minrk I'll open another issue to discuss this, since this is closed :) |
leportella commentedJan 9, 2019
•
edited
So, I created a class called
UserInfo
to store extra information on the user that the default JupyterHubUser
class won't. TheUserInfo
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)
whereself.db
is a SQLAlchemy Engine instance.However, when I try to add a relationship between
UserInfo
andUser
, 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 theUser
table has already been created.Since I am not changing anything on JupyterHub, the
User
table is always created before the moment I create theUserInfo
table, so I can't change this for now.Some things I tried on the process:
User
class before the creation of the new table by using this command:User.__mapper__.add_property('products', relationship("product", collection_class=set))
automap_base
instead of adeclarative_base
, then created theUserInfo
table before by usingself.db.execute(..SQL..)
and then get the table usingautomap
.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.