# 环境python2.7
# 已经创建了blog数据库
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer
#创建引擎
engine = create_engine('mysql+mysqldb://root@localhost:3306/blog?password=admin?charset=utf8')
Base = declarative_base()
# print(engine)
class User(Base):#
__tablename__= 'users'#指定创建的表的名称
id = Column(Integer , primary_key = True)
username = Column(String(64), nullable = False, index = True)
password = Column(String(64), nullable = False)
email = Column(String(64), nullable = False, index = True)
Base.metadata.create_all(engine)
# 除了String 、Integer还有Text 、Boolean、 SmallInteger、 DateTime
创建一对多关系
# 创建一对多的关系,即一个人对应多个博客文章
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy.orm import relationship, backref
#创建引擎
engine = create_engine('mysql+mysqldb://root@localhost:3306/blog?password=admin?charset=utf8')
Base = declarative_base()
# print(engine)
class User(Base):#
__tablename__= 'users'#
id = Column(Integer , primary_key = True)
username = Column(String(64), nullable = False, index = True)
password = Column(String(64), nullable = False)
email = Column(String(64), nullable = False, index = True)
articles = relationship('Article')
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key = True)
title = Column(String(64), nullable = False, index = True)
content = Column(Text)
user_id = Column(Integer, ForeignKey('users.id'))
author = relationship('users')
Base.metadata.create_all(engine)
- 使用backref 只需用在user中表明关系即可,ariticles中就不用在表明关系了
# coding:utf8
# 环境python2.7
# 已经创建了blog数据库
# 创建一对多的关系,即一个人对应多个博客文章
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy.orm import relationship, backref
#创建引擎
engine = create_engine('mysql+mysqldb://root@localhost:3306/blog?password=admin?charset=utf8')
Base = declarative_base()
# print(engine)
class User(Base):#
__tablename__= 'users'#
id = Column(Integer , primary_key = True)
username = Column(String(64), nullable = False, index = True)
password = Column(String(64), nullable = False)
email = Column(String(64), nullable = False, index = True)
# articles = relationship('Article')
articles = relationship('Article', backref = 'author')
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key = True)
title = Column(String(64), nullable = False, index = True)
content = Column(Text)
user_id = Column(Integer, ForeignKey('users.id'))
# author = relationship('users')
Base.metadata.create_all(engine)
一对一关系
- userinfo 存着用户无关紧要的一些数据,一对一是基于一对多的,只增加uselist = False
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy.orm import relationship, backref
#创建引擎
engine = create_engine('mysql+mysqldb://root@localhost:3306/blog?password=admin?charset=utf8')
Base = declarative_base()
# print(engine)
class User(Base):
__tablename__= 'users'#
id = Column(Integer , primary_key = True)
username = Column(String(64), nullable = False, index = True)
password = Column(String(64), nullable = False)
email = Column(String(64), nullable = False, index = True)
# articles = relationship('Article')
articles = relationship('Article', backref = 'author')
userinfo = relationship('UserInfo', backref = 'user', uselist = False) #uselist指明为一对一关系。
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key = True)
title = Column(String(64), nullable = False, index = True)
content = Column(Text)
user_id = Column(Integer, ForeignKey('users.id'))
# author = relationship('users')
class UserInfo(Base):
__tablename__ = 'userinfo'
id = Column(Integer, primary_key = True)
name = Column(String(64))
qq = Column(String(11))
phone = Column(String(11))
link = Column(String(64))
user_id = Column(Integer, ForeignKey('users.id'))
Base.metadata.create_all(engine)
多对多关系
- 多对多需要sqlalchemy的Table类进行支持,如下是创建了标签与文章之间的多对多关系表
from sqlalchemy import create_engine, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy.orm import relationship, backref
#创建引擎
engine = create_engine('mysql+mysqldb://root@localhost:3306/blog?password=admin?charset=utf8')
Base = declarative_base()
# print(engine)
class User(Base):
__tablename__= 'users'#
id = Column(Integer , primary_key = True)
username = Column(String(64), nullable = False, index = True)
password = Column(String(64), nullable = False)
email = Column(String(64), nullable = False, index = True)
# articles = relationship('Article')
articles = relationship('Article', backref = 'author')
userinfo = relationship('UserInfo', backref = 'user', uselist = False) #uselist指明为一对一关系。
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key = True)
title = Column(String(64), nullable = False, index = True)
content = Column(Text)
user_id = Column(Integer, ForeignKey('users.id'))
tags = relationship('Tag', secondary='article_tag', backref='articles')
# author = relationship('users')
class UserInfo(Base):
__tablename__ = 'userinfo'
id = Column(Integer, primary_key = True)
name = Column(String(64))
qq = Column(String(11))
phone = Column(String(11))
link = Column(String(64))
user_id = Column(Integer, ForeignKey('users.id'))
articles_tag = Table(
# 使用此类必须包含表名称及metadata,
'article_tag', Base.metadata,
Column('article_id', Integer, ForeignKey('articles.id')),
Column('tag_id', Integer, ForeignKey('tags.id'))
)#通过此类将article_id与tag_id关联成多对多关系
class Tag(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key = True)
name = Column(String(64), nullable = False, index = True)
Base.metadata.create_all(engine)
通过与mysql数据库建立会话将数据存入数据库
# coding:utf8
# 环境python2.7
# 已经创建了blog数据库
# 创建一对多的关系,即一个人对应多个博客文章
from sqlalchemy import create_engine, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy.orm import relationship, backref, sessionmaker
from faker import Factory
import random
#创建引擎
engine = create_engine('mysql+mysqldb://root@localhost:3306/blog?password=admin?charset=utf8')
Base = declarative_base()
# print(engine)
class User(Base):
__tablename__= 'users'#
id = Column(Integer , primary_key = True)
username = Column(String(64), nullable = False, index = True)
password = Column(String(64), nullable = False)
email = Column(String(64), nullable = False, index = True)
# articles = relationship('Article')
articles = relationship('Article', backref = 'author')
userinfo = relationship('UserInfo', backref = 'user', uselist = False) #uselist指明为一对一关系。
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key = True)
title = Column(String(64), nullable = False, index = True)
content = Column(Text)
user_id = Column(Integer, ForeignKey('users.id'))
tag = relationship('Tag', secondary='article_tag', backref='articles')
# author = relationship('users')
class UserInfo(Base):
__tablename__ = 'userinfos'
id = Column(Integer, primary_key = True)
name = Column(String(64))
qq = Column(String(11))
phone = Column(String(11))
link = Column(String(64))
user_id = Column(Integer, ForeignKey('users.id'))
class Tag(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key = True)
name = Column(String(64), nullable = False, index = True)
articles_tag = Table(
# 使用此类必须包含表名称及metadata,
'article_tag', Base.metadata,
Column('article_id', Integer, ForeignKey('articles.id')),
Column('tag_id', Integer, ForeignKey('tags.id'))
)#通过此类将article_id与tag_id关联成多对多关系
if __name__ == '__main__':
Base.metadata.create_all(engine)
# 创建工厂对象
faker = Factory.create()
# 创建与mysql数据库进会话的链接
Session = sessionmaker(bind = engine)
# 创建链接实例
session = Session()
# 使用faker生成假数据
# 生成用户假数据
faker_users = [User(username = faker.name(), password = faker.word(), email = faker.email()) for i in range(10)]
# 将生成的10条假数据添加到数据库
session.add_all(faker_users)
# 生成标签假数据
faker_tags = [Tag(name = faker.name()) for i in range(5)]
session.add_all(faker_tags)
# 生成100篇文章
for i in range(100):
article = Article(
title = faker.word(),
content = ' '.join(faker.sentences(nb = random.randint(10,20))),#随机生成几句话
author = random.choice(faker_users),
)
for tag in random.sample(faker_tags, random.randint(2,5)):#随机选取2到5个
article.tag.append(tag)
session.add(article)# 添加单个数据到数据库
session.commit()# 提交任务