Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. import sqlalchemy
  2. import datetime
  3. from sqlalchemy import Column, ForeignKey, create_engine
  4. from sqlalchemy.types import (INTEGER, TEXT, VARCHAR, DATETIME)
  5. from sqlalchemy.ext.declarative import declarative_base
  6. from sqlalchemy.orm import relationship
  7.  
  8. Base = declarative_base()
  9.  
  10.  
  11. class Author(Base):
  12. __tablename__ = 'authors'
  13. id = Column(INTEGER, primary_key=True)
  14. name = Column(VARCHAR(30), unique=True, nullable=False)
  15. last_activity = Column(DATETIME, nullable=False)
  16. posts = relationship("Post")
  17. def __repr__(self):
  18. return f'{self.id} {self.name} {self.last_activity}'
  19.  
  20.  
  21. class Post(Base):
  22. __tablename__ = 'posts'
  23. id = Column(INTEGER, primary_key=True)
  24. title = Column(VARCHAR(50), unique=True, nullable=False)
  25. content = Column(TEXT, nullable=False)
  26. author_id = Column(INTEGER, ForeignKey('authors.id'), nullable=False)
  27. published_on = Column(DATETIME, nullable=False)
  28.  
  29. author = relationship("Author")
  30. def __repr__(self):
  31. return f'{self.id} {self.title} {self.content} {self.author_id} {self.published_on}'
  32.  
  33.  
  34. engine = create_engine('sqlite:///blog.db', echo=True)
  35. session = sqlalchemy.orm.Session(bind=engine)
  36.  
  37. Base.metadata.create_all(bind=engine)
  38.  
  39. # author1 = Author(name='Joker', last_activity=datetime.datetime.now())
  40. # author2 = Author(name='MegaAdmin', last_activity=datetime.datetime.now())
  41. # author3 = Author(name='JuniorAdmin', last_activity=datetime.datetime.now())
  42.  
  43. # session.add_all([author1, author2,author3])
  44.  
  45.  
  46. # post1 = Post(title = 'LOL', content = '...1', author_id= 1 , published_on=datetime.datetime.now())
  47. # post2 = Post(title = 'CHIKIBAMBONY', content = '...2', author_id = 2, published_on=datetime.datetime.now())
  48. # post3 = Post(title = 'AWESOME', content = '...3', author_id = 1, published_on=datetime.datetime.now())
  49. # post4 = Post(title = 'CHEBUREK', content = '...4', author_id = 2, published_on=datetime.datetime.now())
  50. # post5 = Post(title = 'TVAR', content = '...5', author_id = 3, published_on=datetime.datetime.now())
  51. # post6 = Post(title = 'GERALD', content = '...6', author_id = 1, published_on=datetime.datetime.now())
  52.  
  53. # # session.add_all([post1,post2,post3,post4,post5,post6])
  54.  
  55. session.commit()
  56.  
  57. # data = session.query(Post).all()
  58. # # data[0].title = "The Best"
  59.  
  60. # p1 = session.query(Post).get(1)
  61. # print (p1)
  62.  
  63.  
  64.  
  65. d = session.query(Author).filter_by(id=2).first()
  66. print (d.posts)
  67.  
  68. # new = Post(title='AL',content = '?',published_on = datetime.datetime.now())
  69. # d.posts.append(new)
  70.  
  71. # session.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement