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 ...
随机推荐
- C语言基础(1)-基本语法及注意事项
1. include 头文件包含 #include <stdio.h>这个是hello world程序的第一句话 # 代表预编译指令 #include的意思就是头文件包含,使用C语言库函数 ...
- ThinkPHP3.2.3整合smarty模板(二)
前言:继ThinkPHP3.2.3整合smarty模板(一)之后,继续来探讨一下tp框架整合smarty模板,看到有人在群上问到怎么使用自定义的常量,今天就具体来谈谈: 一.开发一个项目,必不可少会用 ...
- 大熊君学习html5系列之------History API(SPA单页应用的必备------重构完结版)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- ubuntu12.04server下red5-1.0.0RC1的部署
一.搭建环境 Linux版本:ubuntu12.04sever 64位 Java 版本:Java 1.7(jdk+jre) Red5 版本:red5-1.0.0-RC1 二.安装JDK 下载jdk ...
- UIScrollView的常见属性
@property(nonatomic) CGPoint contentOffset; 这个属性用来表示UIScrollView滚动的位置 (其实就是内容左上角与scrollView左上角的间距值) ...
- 支持向量机SVM
SVM(Support Vector Machine)有监督的机器学习方法,可以做分类也可以做回归.SVM把分类问题转化为寻找分类平面的问题,并通过最大化分类边界点距离分类平面的距离来实现分类. 有好 ...
- Redis学习 - 配置属性:bind
bind这个属性很容易理解成限制可以访问的IP地址,其实是指Redis服务器可以选择监听来自哪个网卡的访问请求.我们再用的时候一般都只有一个网卡,所以只能写本机的IP地址或者回路地址.否则在启动服务器 ...
- BestCoder Round #90
有生以来第一场在COGS以外的地方打的比赛.挂成dog了. 主要是没有经验,加之代码能力过弱.还有最后的瞎hack三次,Too Young Too Simple...... 言归正传. (抄一发题解先 ...
- at 常用命令
以debian 6.0.1 为例: 服务开启关闭: Usage: /etc/init.d/atd {start|stop|restart|force-reload|status} 设置一次计划任务(a ...
- XSS攻击及防御
XSS又称CSS,全称Cross SiteScript,跨站脚本攻击,是Web程序中常见的漏洞,XSS属于被动式且用于客户端的攻击方式,所以容易被忽略其危害性.其原理是攻击者向有XSS漏洞的网站中输入 ...