Hello.

Please take a look at official examples:

http://docs.sqlalchemy.org/en/rel_1_0/orm/backref.html?highlight=back_populates

The first two examples use your classes (User and Address). Note the use of
backref kw arg in the relationship definition (first example) and back_populates
kw arg in the second example. I personally prefer back_populates because each
ORM definition is then complete on its own (i.e. both User and Address will each
define the appropriate relationship connected to the other with back_populates 
kw).


HTH,

Ladislav Lenart


On 11.8.2015 09:59, Sean Lin wrote:
> Dear all,
> 
> I have a question about how to automatic set the foreignkey column ?
> 
> 
> we have two class User and Address 
> 
> |
> 
> classUser(Base):
>     __tablename__ ='user'
>     id =Column(Integer,primary_key=True)
>     name =Column(String)
>     addresses =relationship("Address")
>     def__init__(self,id,name):
>         self.id =id
>         self.name =name
> 
> classAddress(Base):
>     __tablename__ ='address'
>     id =Column(Integer,primary_key=True)
>     user_id =Column(Integer,ForeignKey('user.id'))
>     def__init__(self,id,user_id):
>         self.id =id
>         self.user_id =user_id
> 
> |
> 
> if we create a user then add a address 
> 
> |
> 
> User1=User(1,"Tom")
> Address1=Address('1',None)
> User1.addresses(Address1)
> printUser1.Address[0].user_id
> 
> |
> 
> after that we will get a None result ... 
> 
> How to automatic assign user_id when we add address to a user?


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to