from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
engine = create_engine('mysql+pymysql://root:root@127.0.0.1:3307/test?charset=utf8', echo=True)
Base = declarative_base()
class Address(Base):
"""
CREATE TABLE addresses (
id INTEGER NOT NULL AUTO_INCREMENT,
email_address VARCHAR(30) NOT NULL,
user_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES ttxs_users (id)
)
"""
__tablename__ = 'addresses'
id = Column(Integer, primary_key=True)
email_address = Column(String(30), nullable=False)
user_id = Column(Integer, ForeignKey('ttxs_users.id'))
user = relationship("User", back_populates='addresses')
def __repr__(self):
return "<Address(email_address='%s')>" % self.email_address
class User(Base):
"""
CREATE TABLE ttxs_users (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
fullname VARCHAR(30),
password VARCHAR(30),
PRIMARY KEY (id)
)
"""
__tablename__ = 'ttxs_users'
id = Column(Integer, primary_key=True)
name = Column(String(30))
fullname = Column(String(30))
password = Column(String(30))
addresses = relationship(Address, order_by=Address.id, back_populates="user")
def __repr__(self):
return "<User(name='%s', fullname='%s', password='%s')>" % (self.name, self.fullname, self.password)
@classmethod
def add(cls, **kwargs):
try:
r = cls(**kwargs)
session.add(r)
session.commit()
except:
session.rollback()
raise
Base.metadata.create_all(engine)
ed_user = User(name='ttxsgoto', fullname='ttxsgoto', password='password')
print(ed_user.name, ed_user.id)
ed_user.password = 'f8s7ccs'
ed_user.addresses = [
Address(email_address='ttxsgoto01@163.com'),
Address(email_address='ttxsgoto02@163.com')
]
Session = sessionmaker(bind=engine)
session = Session()
session.add(ed_user)
session.add_all([
User(name='ttxsgoto01', fullname='ttxsgoto01', password='ttxsgoto01'),
User(name='ttxsgoto02', fullname='ttxsgoto02', password='ttxsgoto02'),
User(name='ttxsgoto03', fullname='ttxsgoto03', password='ttxsgoto03')
])
session.flush()
session.commit()
our_user = session.query(User).filter_by(name='ttxsgoto').first()
print(ed_user is our_user)
print(our_user)
print(session.dirty)
print(session.new)
for instance in session.query(User).order_by(User.id):
print(instance.name, instance.fullname, instance.password)
querys = session.query(User).filter(User.name == 'ttxsgoto')
querys = session.query(User).filter(User.name != 'ttxsgoto')
querys = session.query(User).filter(User.name.like('%ttxsgoto%'))
querys = session.query(User).filter(User.name.ilike('%ttxsgoto%'))
querys = session.query(User).filter(User.name.in_(['ttxsgoto01', 'ttxsgoto02']))
querys = session.query(User).filter(~User.name.in_(['ttxsgoto01', 'ttxsgoto02']))
querys = session.query(User).filter(User.name == None)
querys = session.query(User).filter(User.name != None)
querys = session.query(User).filter(User.name == 'ttxsgoto', User.password == 'f8s7ccs')
from sqlalchemy import or_
querys = session.query(User).filter(or_(User.name == 'ttxsgoto', User.name == 'ttxsgoto01'))
querys = session.query(User).filter(User.name.match('ttxsgoto'))
querys = session.query(User).all()
querys = session.query(User).first()
print(querys.name)
querys = session.query(User.id == 13).one()
querys = session.query(User).filter(User.name.like('%ttxsgoto%')).count()
for query in querys:
print('query--->', query.name)
querys =session.query(User, Address).filter(User.id==Address.user_id).filter(Address.email_address=='ttxsgoto01@163.com').all()
querys =session.query(User).join(Address).filter(Address.email_address=='ttxsgoto01@163.com').all()
querys = session.query(User).join(Address, User.id == Address.user_id).all()
for u, a in querys:
print('query--->', u.name, a.email_address)
user = session.query(User).filter_by(id=6).first()
session.delete(user)
session.commit()