业精于勤,荒于嬉;行成于思,毁于随。
跟一個 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 的情況不會寫.