# -*- coding: utf-8 -*-
from sqlalchemy import Column, String, create_engine,ForeignKey,Text,Integer,Table
from sqlalchemy.orm import sessionmaker,relationship
from sqlalchemy.ext.declarative import declarative_base # Create the base class of the object :
Base = declarative_base()
# Initialize database connection :
engine = create_engine('mysql+pymysql://root:root@localhost:3306/test')
# # establish DBSession type :
# DBSession = sessionmaker(bind=engine) # Table(
# 'mapping',
# Base.metadata,
# Column('id',Integer,autoincrement=True,primary_key=True),
# Column('recipe_id',Integer,ForeignKey('recipe.id')),
# Column('ingredient_id',Integer,ForeignKey('ingredient.id'))
# ) arctire_tag = Table(
"arctire_tag",
Base.metadata,
Column('arctire_id',Integer,ForeignKey('arctire.id'),primary_key=True),
Column("tag_id",Integer , ForeignKey("tag.id"),primary_key=True),
) class Arctire(Base):
__tablename__ = "arctire"
id = Column(Integer , primary_key=True , autoincrement=True)
name = Column(String(50) , nullable=False)
tags = relationship("Tag", backref="arctires", secondary='arctire_tag') def __repr__(self):
return "name:%s"%self.name class Tag(Base):
__tablename__ = 'tag'
id = Column(Integer , primary_key=True , autoincrement=True)
tag = Column(String(50) , nullable=False) def __repr__(self):
return "tag:%s"%self.tag Base.metadata.create_all(engine)

# -*- coding: utf-8 -*-
from sqlalchemy import Column, String, create_engine,ForeignKey,Text,Integer,Table
from sqlalchemy.orm import sessionmaker,relationship
from sqlalchemy.ext.declarative import declarative_base # Create the base class of the object :
Base = declarative_base()
# Initialize database connection :
engine = create_engine('mysql+pymysql://root:root@localhost:3306/test')
# # establish DBSession type :
# DBSession = sessionmaker(bind=engine) # arctire_tag = Table(
# "arctire_tag",
# Base.metadata,
# Column('arctire_id',Integer,ForeignKey('arctire.id'),primary_key=True),
# Column("tag_id",Integer , ForeignKey("tag.id"),primary_key=True),
# ) class Arctire_tag(Base):
__tablename__='arctire_tag'
arctire_id=Column(Integer,ForeignKey('arctire.id'),primary_key=True)
tag_id=Column(Integer,ForeignKey('tag.id'),primary_key=True) class Arctire(Base):
__tablename__ = "arctire"
id = Column(Integer , primary_key=True , autoincrement=True)
name = Column(String(50) , nullable=False)
tags = relationship("Tag", backref="arctires", secondary='arctire_tag') def __repr__(self):
return "name:%s"%self.name class Tag(Base):
__tablename__ = 'tag'
id = Column(Integer , primary_key=True , autoincrement=True)
tag = Column(String(50) , nullable=False) def __repr__(self):
return "tag:%s"%self.tag Base.metadata.create_all(engine)

sqlalchemy More articles on many to many relationships

  1. pythonのsqlalchemy Many to many relationship

    Now let's design one that can describe “ The book ” And “ author ” The table structure of the relationship between , Demand is A book can be published by several authors One author can write several books #!/usr/bin/env python from sqlalchemy imp ...

  2. use SQLAlchemy Create one to many , Many to many tables

    The creation of many to many relational tables : If you build a many to many relationship , We can loop through the relationship name , such as laowang = Teacher.query.filter(Teacher.name=='laowang').fi ...

  3. python3 + flask + sqlalchemy +orm(3): Many to many relationship

    There are many in an article tag, One tag It can also belong to multiple articles , Articles and tag There is a many to many relationship config.py DEBUG = True #dialect+driver://root:1q2w3e4r5t@127 ...

  4. SQLAlchemy_ Definition ( one-on-one / One to many / Many to many ) Relationship

    Catalog Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns Basic relational model ...

  5. Flask Database many to many relationship

    Databases use relationships to connect records . among , One to many relationship is the most common type of relationship , It connects a record to a set of related records . When this relationship is realized , To be in “ many ” Add a foreign key on this side , Point to “ One ” This side of the connection records . Most of the other relationships ...

  6. Entity Framework 6 Recipes 2nd Edition(10-9) translate -> Use stored procedures for insertion and deletion in many to many relationships

    10-9. Use stored procedures for insertion and deletion in many to many relationships problem Want to use stored procedures in a no load many to many relationship ( Stored procedures only affect the join table of relationships ) Solution Suppose there is a many to many author ( Author) Watches and books ( B ...

  7. stay thinkphp in , Write blog tags many to many relationship tag frequency statistical algorithm

    I often see other people's blogs , Or there are such random colors in the website , But the font size is related to the frequency of label appearance , So I wanted to write one . As for the random display of colors , That's easy , There's no code here . Because I'm learning thinkphp, So check the data ...

  8. How to solve the problem of hibernate Object transformation in many to many relationships json Dead cycle

    Write down the reasons first ! I wrote SSH project , The problem I encountered in the project is to put the paging object ( That is to say pageBean object ) Turn into json data , Here's the code : public class PageBean <T>{// ...

  9. EF One on one 、 One to many 、 Configuration and cascade deletion of many to many relationships

    This chapter begins to understand EF All kinds of relationships . If you are right about EF I'm not very familiar with the relationships between the entities in the library , Take a look at my ideas , It can help you understand more quickly . I. One to one relationship between entities Add one PersonPhoto class , Represents the user photo class /// < ...

Random recommendation

  1. How to create a RESTful WCF Service

    Original address :http://www.cnblogs.com/jfzhu/p/4044813.html Reprint please indicate the source ( One )web.config file To create a REST WCF Service,endpoin ...

  2. The first 2 Yue di 5 God arc invocation getReturnValue

    http://blog.csdn.net/zengconggen/article/details/38024625

  3. Show textarea There is no line feed in the content

    Show textarea There is no line feed in the content , I've been looking online for a long time , Find solutions in a forum : 1. Store the contents read from the database in a Div Inside , for example : <div class="new-commen ...

  4. sqlserver How to use cursors

    ---- Temporary tables Copy the data to this table , Then do 2 Synchronization of tables SELECT [FA_NUMBER] ,[STATUS] ,[FA_REQUESTOR] ,[CALI_NUMBER] ,[AMT] FROM [ ...

  5. PHP About China basename、dirname、pathinfo Detailed explanation

    basename(url)       Return the filename part of the path . dirname(url)        Return the directory name part of the path . pathinfo(url)       Returns information about the file path . bas ...

  6. LTE Module User Documentation( translate 3)—— Simulation output

    LTE User documentation ( If there is something wrong , Welcome to correct !) 6 Simulation output ns-3 LTE The model currently supports output PHY, MAC, RLC and PDCP Grade Key Performance Indicators ...

  7. MYSQL Learning notes 1 -----mysqladmin -uroot -p status|extended-status

    root@server1 ~]# mysqladmin -uroot -p status -i -r extended-status|grep Handler_commit Enter passwor ...

  8. ios From the album : Get picture information from camera

    ios From the album / Get picture information from camera Get picture information from the album UIImagePickerController *imgPickView = [[UIImagePickerController alloc ...

  9. asp.net utilize ajax and jquery-ui Implementation progress bar

    The front desk with ajax Keep making inquiries , Until the job is done. . The progress bar uses jquery-ui. The background uses the general processing procedure to process the corresponding , Progress information is stored in HttpContext.Application in . Code as a simple example , In practical application, we should deal with ...

  10. CSS background-repeat attribute

    ### cause > There is a problem today , It's just setting up an element width The attribute is 100% after , After putting the mouse on it , It's still only partially shaded , As shown in the figure below : --- After some thought , this TM No, it is. ,hover On ...