业精于勤,荒于嬉;行成于思,毁于随。
跟一个 Post 有多个 Tags 的情况是一样的.
--- 更新
我发现我也不会写,\汗
之前以为是 Handling Multiple Join Paths 的问题. 通过指定 foreign_keys 可以避免冲突. relationship("Address",foreign_keys=[billing_address_id])
relationship("Address",foreign_keys=[billing_address_id])
但是只有一个 foreign_keys 的情况不会写.
python#!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import create_engine engine = create_engine("mysql://root:root@localhost/think", echo=True) from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() from sqlalchemy import Column, Integer, SmallInteger, String , DateTime, ForeignKey from sqlalchemy.orm import relationship, backref from datetime import datetime # Post class Post(Base): __tablename__ = 'Post' id = Column(Integer, primary_key=True) title = Column(String(100), index=True) content = Column(String(100), index=True) pics = relationship("Pic", backref=backref('Post', order_by=id)) date = Column(DateTime, default=datetime.now()) def __init__(self, title, content): self.title = title self.content = content def __repr__(self): return "{title:%s,title_pic:%s,content:%s,content_pic:%s}" \ % (self.title,self.pics[0],self.content,self.pics[1:]) # 图片 class Pic(Base): __tablename__ = 'Pic' id = Column(Integer, primary_key=True) name = Column(String(10), index=True, unique=True) data = Column(String(100), index=True, unique=True) post_id = Column(Integer, ForeignKey('Post.id')) def __init__(self, name, data): self.name = name self.data = data def __repr__(self): return "{name:%s,data:%s}" % (self.name,self.data) if '__main__' == __name__ : from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=engine) session = Session() # 1 Base.metadata.drop_all(engine) Base.metadata.create_all(engine) # 2 one = Post("h1", "lalala") one.pics.append(Pic("title_pic","img0.bmp")) one.pics.append(Pic("p0","img1.bmp")) one.pics.append(Pic("p1","img2.bmp")) one.pics.append(Pic("p2","img3.bmp")) two = Post("h2", "hahaha") two.pics.append(Pic("title_pic2","img20.bmp")) two.pics.append(Pic("p20","img21.bmp")) two.pics.append(Pic("p21","img22.bmp")) two.pics.append(Pic("p22","img23.bmp")) session.add_all([one,two]) session.commit() # 3 ret = session.query(Post).filter(Post.title == "h2").one() print ret session.close()
python
#!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import create_engine engine = create_engine("mysql://root:root@localhost/think", echo=True) from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() from sqlalchemy import Column, Integer, SmallInteger, String , DateTime, ForeignKey from sqlalchemy.orm import relationship, backref from datetime import datetime # Post class Post(Base): __tablename__ = 'Post' id = Column(Integer, primary_key=True) title = Column(String(100), index=True) content = Column(String(100), index=True) pics = relationship("Pic", backref=backref('Post', order_by=id)) date = Column(DateTime, default=datetime.now()) def __init__(self, title, content): self.title = title self.content = content def __repr__(self): return "{title:%s,title_pic:%s,content:%s,content_pic:%s}" \ % (self.title,self.pics[0],self.content,self.pics[1:]) # 图片 class Pic(Base): __tablename__ = 'Pic' id = Column(Integer, primary_key=True) name = Column(String(10), index=True, unique=True) data = Column(String(100), index=True, unique=True) post_id = Column(Integer, ForeignKey('Post.id')) def __init__(self, name, data): self.name = name self.data = data def __repr__(self): return "{name:%s,data:%s}" % (self.name,self.data) if '__main__' == __name__ : from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=engine) session = Session() # 1 Base.metadata.drop_all(engine) Base.metadata.create_all(engine) # 2 one = Post("h1", "lalala") one.pics.append(Pic("title_pic","img0.bmp")) one.pics.append(Pic("p0","img1.bmp")) one.pics.append(Pic("p1","img2.bmp")) one.pics.append(Pic("p2","img3.bmp")) two = Post("h2", "hahaha") two.pics.append(Pic("title_pic2","img20.bmp")) two.pics.append(Pic("p20","img21.bmp")) two.pics.append(Pic("p21","img22.bmp")) two.pics.append(Pic("p22","img23.bmp")) session.add_all([one,two]) session.commit() # 3 ret = session.query(Post).filter(Post.title == "h2").one() print ret session.close()
跟一个 Post 有多个 Tags 的情况是一样的.
--- 更新
我发现我也不会写,\汗
之前以为是 Handling Multiple Join Paths 的问题. 通过指定 foreign_keys 可以避免冲突.
relationship("Address",foreign_keys=[billing_address_id])
但是只有一个 foreign_keys 的情况不会写.