python 之sqlalchemy many to one
通过查询多个父亲,对应一个儿子
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: zengchunyun
"""
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine Base = declarative_base()
engine = create_engine('mysql+pymysql://root:123@127.0.0.1:3306/day11',echo=True) class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String(64))
child_id = Column(Integer, ForeignKey("child.id"))
childr = relationship("Child") class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String(64)) Base.metadata.create_all(engine) DBSession = sessionmaker()
DBSession.configure(bind=engine)
session = DBSession() # 打开数据连接 ret = session.query(Parent).filter(Parent.name == 'zeng').one()
print(ret)
print(ret.childr.name)
ret = session.query(Parent).filter(Parent.name == 'chunyun').one()
print(ret)
print(ret.childr.name) ret = session.query(Parent).filter(Parent.name == 'chun').one()
print(ret)
print(ret.childr.name)
many to one 第二式
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String(64))
child_id = Column(Integer, ForeignKey("child.id"))
childr = relationship("Child", back_populates="parents") class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String(64))
parents = relationship("Parent", back_populates="childr")找到父亲 #通过双向建立映射关系,能让父亲查到儿子,也能让儿子
many to one 终极版
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String(64))
child_id = Column(Integer, ForeignKey("child.id"))
childr = relationship("Child", backref="parents") # 这段代码,变相的等于在Child类中添加了parents = relationship("Parent", back_populates="childr") class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String(64))
python 之sqlalchemy many to one的更多相关文章
- 基于Python的SQLAlchemy的操作
安装 在Python使用SQLAlchemy的首要前提是安装相应的模块,当然作为python的优势,可以到python安装目录下的scripts下,同时按住shift+加上鼠标左键,从而在菜单中打开命 ...
- SQLAlchemy(1) -- Python的SQLAlchemy和ORM
Python的SQLAlchemy和ORM(object-relational mapping:对象关系映射) web编程中有一项常规任务就是创建一个有效的后台数据库.以前,程序员是通过写sql语句, ...
- python使用sqlalchemy连接pymysql数据库
python使用sqlalchemy连接mysql数据库 字数833 阅读461 评论0 喜欢1 sqlalchemy是python当中比较出名的orm程序. 什么是orm? orm英文全称objec ...
- python之SQLAlchemy
ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为 ...
- Python’s SQLAlchemy vs Other ORMs[转发 7] 比较结论
Comparison Between Python ORMs For each Python ORM presented in this article, we are going to list t ...
- Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy
SQLAlchemy SQLAlchemy is an open source SQL toolkit and ORM for the Python programming language rele ...
- Python’s SQLAlchemy vs Other ORMs[转发 3]Django's ORM
Django's ORM Django is a free and open source web application framework whose ORM is built tightly i ...
- Python’s SQLAlchemy vs Other ORMs[转发 2]Storm
Storm Storm is a Python ORM that maps objects between one or more databases and Python. It allows de ...
- Python’s SQLAlchemy vs Other ORMs[转发 0]
原文地址:http://pythoncentral.io/sqlalchemy-vs-orms/ Overview of Python ORMs As a wonderful language, Py ...
- Python’s SQLAlchemy vs Other ORMs[转发 1]SQLObject
SQLObject SQLObject is a Python ORM that maps objects between a SQL database and Python. It is becom ...
随机推荐
- MySql 里的IFNULL、NULLIF和ISNULL用法区别
mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法:如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0. mysql> ...
- golang笔记——函数与方法
如果你遇到没有函数体的函数声明,表示该函数不是以Go实现的. package math func Sin(x float64) float //implemented in assembly lang ...
- Java总结篇系列:Java多线程(一)
多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程 ...
- Asp.Net Core--授权介绍
翻译如下: 授权指的是确定一个用户能够做什么的过程.例如用户可以Adam能够创建一个文档库,添加文档,编辑文档并将其删除.用户Bob可能只被授权在单个库中读取文件. 授权与验证,这是查明谁一个用户的过 ...
- ToolStripMenuItem
MenuStrip 类 为窗体提供菜单系统. 继承层次结构 System.Object System.MarshalByRefObject System.ComponentModel.Comp ...
- BestCoder Round #90
有生以来第一场在COGS以外的地方打的比赛.挂成dog了. 主要是没有经验,加之代码能力过弱.还有最后的瞎hack三次,Too Young Too Simple...... 言归正传. (抄一发题解先 ...
- java4
1:如何制作帮助文档(了解) (1)写一个类 (2)加入文档注释 (3)通过javadoc工具生成即可 javadoc -d 目录 -author -version ArrayTool.java 2: ...
- testng教程之testng.xml的配置和使用,以及参数传递
昨天学习了一下testng基础教程,http://www.cnblogs.com/tobecrazy/p/4579414.html 昨天主要学习的是testng 的annotation基本用法和生命周 ...
- 实现tableview的下拉刷新
实现tableview的下拉刷新 推荐第三方下拉刷新代码http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6 ...
- TCP协议三次握手和四次挥手
http://www.cnblogs.com/rootq/articles/1377355.html TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对 ...