python - SQLAlchemy multiple foreight keys -
can explain proper way :
class bracket(base): __tablename__ = 'weekly_bracket' bracket_1a_winner = column( integer, nullable = false ) bracket_1b_winner = column( integer, nullable = false ) bracket_2_winner = column( integer, nullable = false ) handle = column( string(250),foreignkey('user.site_handle') ) user_id = column( integer, foreignkey('user.id'), primary_key = true ) user = relationship( user )
in regard multiple foreign keys, can check out multiple join paths in docs further information. explain in more detail going on.
for immediate concern, there problems class. you're primary key set foreign key. not trying to. table name in relationship()
should name of user class, string. can specify foreign key in relationship() call. try this:
class bracket(base): __tablename__ = 'weekly_bracket' id = column ( integer, primary_key = true ) bracket_1a_winner = column( integer, nullable = false ) bracket_1b_winner = column( integer, nullable = false ) bracket_2_winner = column( integer, nullable = false ) handle = column( string(250),foreignkey('user.site_handle') ) user_id = column( integer, foreignkey('user.id') ) user = relationship( 'user', foreign_keys=[user_id] )
in case, don't see need multiple foreign keys here. if had 2 users, 2 relationships, cause multiple foreign keys. example:
class bracket(base): __tablename__ = 'weekly_bracket' id = column ( integer, primary_key = true ) bracket_1a_winner = column( integer, nullable = false ) bracket_1b_winner = column( integer, nullable = false ) bracket_2_winner = column( integer, nullable = false ) handle = column( string(250),foreignkey('user.site_handle') ) bracket1_user_id = column( integer, foreignkey('user.id') ) bracket2_user_id = column( integer, foreignkey('user.id') ) bracket1_user = relationship( 'user', foreign_keys=[bracket1_user_id] ) bracket2_user = relationship( 'user', foreign_keys=[bracket2_user_id] )
as @christopherpearson mentioned, don't need 'id' field acting primary_key(i deleted incorrect comment). using user_id primary key ok because user_id field unique. if want user have more 1 bracket, relationship won't work long.
Comments
Post a Comment