Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. import sqlalchemy
  2. from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import relationship, sessionmaker
  5. import os
  6.  
  7. engine = create_engine('sqlite:///:memory:', echo=True)
  8.  
  9. Base = declarative_base()
  10.  
  11. class User(Base):
  12.     __tablename__ = 'users'
  13.  
  14.     id = Column(Integer, primary_key=True)
  15.     username = Column(String(64), unique=True)
  16.  
  17.     posts = relationship(
  18.         "Post",
  19.         back_populates='author',
  20.         cascade="all, delete, delete-orphan"
  21.     )
  22.  
  23.  
  24. class Post(Base):
  25.     __tablename__ = 'posts'
  26.     id = Column(Integer, primary_key=True)
  27.     body = Column(String(120))
  28.  
  29.     user_id = Column(Integer, ForeignKey('users.id'))
  30.  
  31.     author = relationship("User", back_populates = "posts")
  32.  
  33.  
  34. Base.metadata.create_all(engine)
  35.  
  36. Session = sessionmaker(bind=engine, autoflush=False)
  37. session = Session()
  38.  
  39. j = User(username='john')
  40. s = User(username='susan')
  41. p1 = Post(body='first', author=j)
  42. p2 = Post(body='second', author=j)
  43. p3 = Post(body='third', author=s)
  44. session.add_all([
  45.     j, s, p1, p2, p3
  46. ])
  47. session.commit()
  48. os.system('clear')
  49. print()
  50.  
  51. print('session.query(Post).filter_by(author=j).all()')
  52. print('----------------------------------------------------------------')
  53. print(session.query(Post).filter_by(author=j).all())
  54. print()
  55.  
  56. print('================================================================')
  57. print('================================================================')
  58. print('================================================================')
  59. print()
  60. print()
  61.  
  62.  
  63. print('session.query(Post).filter_by(author=j).all()')
  64. print('----------------------------------------------------------------')
  65. print(session.query(Post).filter_by(author=j).all())
  66. print()
  67.  
  68. print('session.query(Post).with_parent(j).all()')
  69. print('----------------------------------------------------------------')
  70. print(session.query(Post).with_parent(j).all())
  71. print()
  72.  
  73. print('session.query(Post).with_parent(j, property="posts").all()')
  74. print('----------------------------------------------------------------')
  75. print(session.query(Post).with_parent(j, property='posts').all())
  76. print()
  77.  
  78. print('session.query(Post).with_parent(j, property=User.posts).all()')
  79. print('----------------------------------------------------------------')
  80. print(session.query(Post).with_parent(j, property=User.posts).all())
  81. print()
  82.  
  83. print('j.posts')
  84. print('----------------------------------------------------------------')
  85. print(j.posts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement