zoukankan      html  css  js  c++  java
  • day40 ORM sqlalchemy

    一对多(2017-8-2 16:11:42)

    import sqlalchemy
    from sqlalchemy import  create_engine,and_,or_,func,Table
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String,ForeignKey
    from  sqlalchemy.orm import sessionmaker,relationship
    engine=create_engine('mysql+pymysql://root:123@127.0.0.1:3306/tt?charset=utf8',echo=True)
    #有中文的要加charset
    Base=declarative_base()
    
    class Father(Base):
        __tablename__ = 'father'
        id= Column(Integer,primary_key=True)
        name = Column(String(20))
        age = Column(Integer)
        def __repr__(self):
            return "<Father(name='%s')>" % self.name
    
    class Son(Base):
        __tablename__= 'son'
        id = Column(Integer,primary_key=True)
        name = Column(String(20))
        father_id = Column(Integer, ForeignKey('father.id')) #外键建在数量更多的一方
        father = relationship("Father",backref="son",order_by=id) # 可能要先建立关系 下面才能在表中联系
        def __repr__(self):
            return "<Son(name='%s')>" % self.name
    #建表时要注意顺序,被加外键的要建在前面
    #出现外键错误的时候一般:1.两张表里要设主键和外键的字段的数据类型或者数据长度不一样;2.其中一个表里有数据了;
    Base.metadata.create_all(engine) #创建表格
    # Base.metadata.drop_all(engine) #删除表格
    
    # 插入数据
    Session = sessionmaker(bind=engine)
    session = Session()
    
    f1=Father(name='ge',age=12)
    s1=Son(name='wew',father_id=1)
    
    session.add_all([f1,s1])# 如果有多个,可以一起加在同一个表中;
    session.commit()
    View Code
    import sqlalchemy
    from sqlalchemy import  create_engine,and_,or_,func,Table
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String,ForeignKey
    from  sqlalchemy.orm import sessionmaker,relationship
    engine=create_engine('mysql+pymysql://root:123@127.0.0.1:3306/tt?charset=utf8',echo=False)
    #有中文的要加charset
    Base=declarative_base()
    
    class Father(Base):
        __tablename__ = 'father'
        id= Column(Integer,primary_key=True)
        name = Column(String(20))
        age = Column(Integer)
        def __repr__(self):
            return "<Father(name='%s')>" % self.name
    
    class Son(Base):
        __tablename__= 'son'
        id = Column(Integer,primary_key=True)
        name = Column(String(20))
        father_id = Column(Integer, ForeignKey('father.id')) #外键建在数量更多的一方
        father = relationship("Father",backref="son",order_by=id) # 可能要先建立关系
        def __repr__(self):                                         #下面才能在表中联系 backrefs 反向的relationship
            return "<Son(name='%s')>" % self.name
    #建表时要注意顺序,被加外键的要建在前面
    #出现外键错误的时候一般:1.两张表里要设主键和外键的字段的数据类型或者数据长度不一样;2.其中一个表里有数据了;
    Base.metadata.create_all(engine) #创建表格
    # Base.metadata.drop_all(engine) #删除表格
    
    # 插入数据
    Session = sessionmaker(bind=engine)
    session = Session()
    
    # r1=session.query(Father).filter_by(id=1).first() # .first 是拿到第一个结果 类型是对象
                       # filter()里面放条件判断     # .all是拿到一个列表
    #relationship
    
    # 关联查询
    # ret=session.query(Father.name,Son.id.label('kk')).join(Son)
    # .label是命名
    # 不加first 或者all 打印出来的是一条sql语句
    # print(ret)
    # SELECT father.name AS father_name, son.id AS son_id
    # FROM father INNER JOIN son ON father.id = son.father_id
    # f1=Father(name='ge',age=12)
    # s1=Son(name='wew',father_id=1)
    
    # session.add_all([f1,s1])# 如果有多个,可以一起加在同一个表中;
    session.commit()
    View Code
  • 相关阅读:
    定时任务时间表达式的规则(自己总结)
    本地vagrant配置虚拟域名的坑
    商派onex本地部署无法进入的问题
    一周一篇文章,立贴为证
    Ecshop安装的坑,建议不要使用!
    MYSQL查询语句优化
    .gitignore文件
    剖析Disruptor:为什么会这么快?(二)神奇的缓存行填充
    Disruptor 为什么这么快?
    一篇文章让你成为 NIO 大师 - MyCAT通信模型
  • 原文地址:https://www.cnblogs.com/ezway/p/7274525.html
Copyright © 2011-2022 走看看