Flask框架之ORM层面外键和多对多关系

ORM层面外键和多对多关系

  1. 多对多的关系需要通过一张中间表来绑定他们之间的关系。
  2. 先把两个需要做多对多的模型定义出来
  3. 使用Table定义一个中间表,中间表一般就是包含两个模型的外键字段就可以了,并且让它们两个来作为一复合主键
  4. 在两个需要做多对多的模型中随便选择一个模型,定义一个relationship属性,来绑定三者之间的关系,在使用relationship的时候,需要传入一个secondary=中间表对象名

from sqlalchemy import create_engine,Column,Integer,Float,Boolean,DECIMAL,Enum,\
	 Date,DateTime,Time,String,Text,func,or_,and_,ForeignKey,Table 
from sqlalchemy.dialects.mysql import LONGTEXT 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import sessionmaker,relationship,backref 
import random 

HOSTNAME = '127.0.0.1' 
PORT = '3306' 
DATABASE = 'first_sqlalchemy' 
USERNAME = 'root' 
PASSWORD = 'root' 
DB_URI ="mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8"\
	.format(username=USERNAME,password=PASSWORD,host=HOSTNAME,port=PORT,db=DATABASE)

engine = create_engine(DB_URI) 

Base = declarative_base(engine) 

session = sessionmaker(engine)() 

#表3 中间表 
news_tag = Table( "news_tag", Base.metadata,
	Column("news_id",Integer,ForeignKey("news.id"),primary_key=True),
	Column("tag_id",Integer,ForeignKey("tag.id"),primary_key=True) ) 

#表1 
class News(Base): 
	__tablename__ = 'news' 
	id = Column(Integer,primary_key=True,autoincrement=True) 
	title = Column(String(50),nullable=False) 

	def __repr__(self): 
		return "<News(title:%s)>" % self.title 

#表2 
class Tag(Base): 
	__tablename__ = 'tag' 
	id = Column(Integer, primary_key=True, autoincrement=True) 
	name = Column(String(50), nullable=False) 
	# 产生关系
	newss = relationship("News",backref="tags",secondary=news_tag) 
	def __repr__(self): 
		return "<Tag(name:%s)>" % self.name 
	
Base.metadata.drop_all() 
Base.metadata.create_all() 

发布了364 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44733660/article/details/104083266