#-*-coding:utf-8-*-
#__author__ = "logan.xu" import sqlalchemy
from sqlalchemy import create_engine,func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String,ForeignKey,DATE
from sqlalchemy.orm import sessionmaker,relationship engine=create_engine("mysql+pymysql://root:12345678@localhost/news",encoding='utf-8',echo=True)
Base=declarative_base() class Student(Base):
__tablename__='student'
id=Column(Integer,primary_key=True)
name=Column(String(32),nullable=False)
register_date=Column(DATE,nullable=False) def __repr__(self):
return "<%s name:%s>" % (self.id,self.name) class StudyRecord(Base):
__tablename__ = 'study_record'
id = Column(Integer, primary_key=True)
day = Column(Integer,nullable=False)
status = Column(String(32),nullable=False)
stu_id = Column(Integer,ForeignKey("student.id")) def __repr__(self):
return "<%s name:%s>" % (self.id,self.day) #---------------1.创建表-----------------#

Base.metadata.create_all(engine)

#---------------------------------------# Session_class = sessionmaker(bind=engine)
Session = Session_class() #---------------2.写入数据-----------------# s1=Student(name='Alex',register_date="2019-01-09")
s2=Student(name='Jack',register_date="2019-01-09")
s3=Student(name='Rain',register_date="2019-01-10")
s4=Student(name='Eric',register_date="2019-01-11")
s5=Student(name='John',register_date="2019-01-12")
#
study_obj1=StudyRecord(day=1,status="Yes",stu_id=1)
study_obj2=StudyRecord(day=2,status="NO",stu_id=1)
study_obj3=StudyRecord(day=3,status="Yes",stu_id=1)
study_obj4=StudyRecord(day=1,status="NO",stu_id=2)
study_obj5=StudyRecord(day=1,status="NO",stu_id=2)
# ## 写入数据 并调用 study_record字段插入数据 为 Student数据

Session.add_all([s1,s2,s3,s4,s5,study_obj1,study_obj2,study_obj3,study_obj4,study_obj5]) #---------------------------------------# # 执行事务
Session.commit()
/Users/drizzle/PycharmProjects/2018-12-03/venv/bin/python /Users/drizzle/PycharmProjects/2018-12-03/orm_fk.py
2019-01-08 18:08:40,496 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2019-01-08 18:08:40,496 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,498 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2019-01-08 18:08:40,498 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,500 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2019-01-08 18:08:40,500 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,501 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'
2019-01-08 18:08:40,501 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,504 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2019-01-08 18:08:40,504 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,504 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2019-01-08 18:08:40,504 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,505 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS
CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1
2019-01-08 18:08:40,505 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,506 INFO sqlalchemy.engine.base.Engine DESCRIBE `student`
2019-01-08 18:08:40,506 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,507 INFO sqlalchemy.engine.base.Engine DESCRIBE `study_record`
2019-01-08 18:08:40,507 INFO sqlalchemy.engine.base.Engine {}
2019-01-08 18:08:40,511 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-01-08 18:08:40,512 INFO sqlalchemy.engine.base.Engine INSERT INTO student (name, register_date) VALUES (%(name)s, %(register_date)s)
2019-01-08 18:08:40,512 INFO sqlalchemy.engine.base.Engine {'name': 'Alex', 'register_date': '2019-01-09'}
2019-01-08 18:08:40,513 INFO sqlalchemy.engine.base.Engine INSERT INTO student (name, register_date) VALUES (%(name)s, %(register_date)s)
2019-01-08 18:08:40,513 INFO sqlalchemy.engine.base.Engine {'name': 'Jack', 'register_date': '2019-01-09'}
2019-01-08 18:08:40,513 INFO sqlalchemy.engine.base.Engine INSERT INTO student (name, register_date) VALUES (%(name)s, %(register_date)s)
2019-01-08 18:08:40,514 INFO sqlalchemy.engine.base.Engine {'name': 'Rain', 'register_date': '2019-01-10'}
2019-01-08 18:08:40,514 INFO sqlalchemy.engine.base.Engine INSERT INTO student (name, register_date) VALUES (%(name)s, %(register_date)s)
2019-01-08 18:08:40,514 INFO sqlalchemy.engine.base.Engine {'name': 'Eric', 'register_date': '2019-01-11'}
2019-01-08 18:08:40,515 INFO sqlalchemy.engine.base.Engine INSERT INTO student (name, register_date) VALUES (%(name)s, %(register_date)s)
2019-01-08 18:08:40,515 INFO sqlalchemy.engine.base.Engine {'name': 'John', 'register_date': '2019-01-12'}
2019-01-08 18:08:40,516 INFO sqlalchemy.engine.base.Engine INSERT INTO study_record (day, status, stu_id) VALUES (%(day)s, %(status)s, %(stu_id)s)
2019-01-08 18:08:40,516 INFO sqlalchemy.engine.base.Engine {'day': 1, 'status': 'Yes', 'stu_id': 1}
2019-01-08 18:08:40,517 INFO sqlalchemy.engine.base.Engine INSERT INTO study_record (day, status, stu_id) VALUES (%(day)s, %(status)s, %(stu_id)s)
2019-01-08 18:08:40,517 INFO sqlalchemy.engine.base.Engine {'day': 2, 'status': 'NO', 'stu_id': 1}
2019-01-08 18:08:40,517 INFO sqlalchemy.engine.base.Engine INSERT INTO study_record (day, status, stu_id) VALUES (%(day)s, %(status)s, %(stu_id)s)
2019-01-08 18:08:40,517 INFO sqlalchemy.engine.base.Engine {'day': 3, 'status': 'Yes', 'stu_id': 1}
2019-01-08 18:08:40,518 INFO sqlalchemy.engine.base.Engine INSERT INTO study_record (day, status, stu_id) VALUES (%(day)s, %(status)s, %(stu_id)s)
2019-01-08 18:08:40,518 INFO sqlalchemy.engine.base.Engine {'day': 1, 'status': 'NO', 'stu_id': 2}
2019-01-08 18:08:40,518 INFO sqlalchemy.engine.base.Engine INSERT INTO study_record (day, status, stu_id) VALUES (%(day)s, %(status)s, %(stu_id)s)
2019-01-08 18:08:40,518 INFO sqlalchemy.engine.base.Engine {'day': 1, 'status': 'NO', 'stu_id': 2}
2019-01-08 18:08:40,519 INFO sqlalchemy.engine.base.Engine COMMIT Process finished with exit code 0
mysql> show tables;
+----------------+
| Tables_in_news |
+----------------+
| address |
| customer |
| student |
| study_record |
| user |
+----------------+
5 rows in set (0.01 sec) mysql> desc student;
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | NO | | NULL | |
| register_date | date | NO | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) mysql> desc study_record;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| day | int(11) | NO | | NULL | |
| status | varchar(32) | NO | | NULL | |
| stu_id | int(11) | YES | MUL | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec) mysql> select * from student;
Empty set (0.00 sec) mysql> select * from stuy_record;
ERROR 1146 (42S02): Table 'news.stuy_record' doesn't exist
mysql>
mysql> select * from study_record;
Empty set (0.00 sec) mysql> show tables;
+----------------+
| Tables_in_news |
+----------------+
| address |
| customer |
| student |
| study_record |
| user |
+----------------+
5 rows in set (0.00 sec) mysql> show create table student;
+---------+-------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`register_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+-------------------------------------------------------------+
1 row in set (0.00 sec) mysql> show create table study_record;
+--------------+-------------------------------------------------------------------------+
| Table | Create Table |
+--------------+-------------------------------------------------------------------------+
| study_record | CREATE TABLE `study_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`day` int(11) NOT NULL,
`status` varchar(32) NOT NULL,
`stu_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `stu_id` (`stu_id`),
CONSTRAINT `study_record_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `student` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------------+------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> select * from study_record;
+----+-----+--------+--------+
| id | day | status | stu_id |
+----+-----+--------+--------+
| 2 | 1 | Yes | 1 |
| 3 | 2 | NO | 1 |
| 4 | 3 | Yes | 1 |
| 5 | 1 | NO | 2 |
| 6 | 1 | NO | 2 |
+----+-----+--------+--------+
5 rows in set (0.00 sec) mysql> select * from student;
+----+------+---------------+
| id | name | register_date |
+----+------+---------------+
| 1 | Alex | 2019-01-09 |
| 2 | Jack | 2019-01-09 |
| 3 | Rain | 2019-01-10 |
| 4 | Eric | 2019-01-11 |
| 5 | John | 2019-01-12 |
+----+------+---------------+
5 rows in set (0.00 sec) mysql>

Python3-sqlalchemy-orm 创建关联表带外键并插入数据的更多相关文章

  1. Python3-sqlalchemy-orm 创建关联表带外键并查询数据

    #-*-coding:utf-8-*- #__author__ = "logan.xu" import sqlalchemy from sqlalchemy import crea ...

  2. Python3-sqlalchemy-orm 创建多表关联表带外键

    #-*-coding:utf-8-*- #__author__ = "logan.xu" import sqlalchemy from sqlalchemy import crea ...

  3. 《Entity Framework 6 Recipes》中文翻译系列 (37) ------ 第六章 继承与建模高级应用之独立关联与外键关联

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-13  在基类中应用条件 问题 你想从一个已存在的模型中的实体派生一个新的实体, ...

  4. 数据库的SQL语句创建和主外键删除操作

    create table UserType ( Id ,), Name nvarchar() not null ) go create table UserInfo ( Id ,), LoginPwd ...

  5. mysql数据库:分表、多表关联、外键约束、级联操作

    一.分表.外键.级联.多对一 二.多对多 三.一对一 一.分表.外键.级联.多对一 将部门数据与员工数据放到同一张表中会造成 数据重复 结构混乱 扩展维护性差 需要分表 create table de ...

  6. 003--PowerDesigner创建索引与外键

    PowerDesigner创建索引与外键 一.创建索引 双击Table->Columns->创建索引 Step1:双击Table Step2:选择Columns->创建索引 弹出如下 ...

  7. SQLSERVER清空(Truncate)被外键引用的数据表

    前言:我们知道SQLSERVER清空数据表有两种方式Delete和Truncate,当然两者的不同大家也都知道(不清楚的可以MSDN).不过这个错误“Cannot truncate table  be ...

  8. 使用Navicat V8.0创建数据库,外键出现错误ERROR 1005: Can’t create table (errno: 121)

    ERROR 1005: Can't create table (errno: 121) errno 121 means a duplicate key error. Probably the tabl ...

  9. Hibernate一对一双向关联(外键)

    网站上各种搜索,都是一些清晰或者不清晰的例子,但是用下来一是确实不给力,二是完全不知道所以然. 后来终于在书中查到了就在这里记一下. 首先要说明,这里只解释双向一对一只有一个表里面有外键的情况. 就以 ...

随机推荐

  1. 第十三天 -- 如何用U盘重装系统Win10以及如何用VMware12安装Win10

    U盘制作启动盘 1.在电脑上插入U盘,关闭安全软件杀毒工具,然后打开装机吧U盘启动盘制作工具 2.选择刚插入的U盘,勾选上,点击一键制作启动U盘,制作前U盘数据必须转移备份: 3.选择格式化U盘,记得 ...

  2. 2021年BI软件排名,国内外BI软件功能对比

    数据分析是帮助企业深入了解自身业务表现(例如正在做什么或哪块业务需要注意和改进)的重要元素.为了获得更直观的展现,数据分析BI软件可帮助公司通过报告.数据可视化.应用程序等从数据中获取决策数据支撑.没 ...

  3. Querydsl与SpringBoot集成

    Querydsl为大多数数据库提供了一种基于Java的类型安全,类SQL的查询方式.相比JPA,Querydsl能提供更加强大的查询方式,比如关联查询.相比MyBatis,Querydsl省去了XML ...

  4. 创建函数,传递一个数字n,返回斐波那契数列的第n的值。

    斐波那契数列 第1项和第2项的值是1,从第3项开始,每项的值是前两项相加的和 1   1   2    3    5    8    13    21...... 法1: function fn(n) ...

  5. C/C++入门

    C:面向过程-函数 C++:面向对象-类:向下兼容C 程序包含头文件+主函数 简单数据类型 long long型赋超过int型范围的初值,需要在初值后面加上LL,否则可能会编译错误 不要使用float ...

  6. 京东购物小程序 | Taro3 项目分包实践

    背景 京东购物小程序作为京东小程序业务流量的主要入口,承载着许多的活动和页面,而很多的活动在小程序开展的同时,也会在京东 APP 端进行同步的 H5 端页面的投放.这时候,一个相同的活动,需要同时开发 ...

  7. Mybatis学习笔记-配置解析

    核心配置文件 mybatis-config.xml properties(属性) settings(设置) typeAliases(类型别名) typeHandlers(类型处理器) objectFa ...

  8. 解决:无法从 Windows 应用商店下载。请检查网络连接

    今天在安装wsl的时候,装了几次一直中断,一直提示正在从Windows 应用商店下载,网络无法连接... 每次都是加载到2%就断了.网上搜了一圈,找到以下2种解决办法: 修改DNS把DNS修改为微软的 ...

  9. Vulhub-DC-1靶场

    前言: DC-1靶场是Vulhub系一款渗透测试的实战靶场,拥有五个Flag.本篇博客讲述了如何拿去这五个Flag,并详细描述其中的注意点. 实验环境: 虚拟机环境:virtualbox 虚拟机1:D ...

  10. 如何将fidd上抓的包移到jmete中

    1.fiddler的安装配置就不说了, 网上有很多资源, 不会太难 2.使用fiddler抓包, 相信进来看这篇文章的博友都已经会使用fiddler抓包 3.打开jmeter, 添加>测试计划& ...