Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import sqlalchemy
  2. from sqlalchemy import Column, create_engine, ForeignKey
  3. from sqlalchemy.types import Integer, VARCHAR, TEXT, DateTime
  4. from sqlalchemy.ext.declarative import declarative_base
  5.  
  6. Base = declarative_base()
  7.  
  8. class Author(Base):
  9. __tablename__ = 'authors'
  10.  
  11. id = Column(Integer, primary_key=True)
  12. name = Column(VARCHAR(30), unique=True, nullable=False)
  13. updated_on = Column(DateTime, nullable=False)
  14.  
  15. def __repr__(self):
  16. return f'{self.id}\t{self.name}\t{self.updated_on}\n'
  17.  
  18.  
  19. class Post(Base):
  20. __tablename__ = 'posts'
  21.  
  22. id = Column(Integer, primary_key=True)
  23. title = Column(VARCHAR(50), unique=True, nullable=False)
  24. content = Column(TEXT, unique=True, nullable=False)
  25. author_id = Column(Integer, ForeignKey('authors.id'), nullable=False)
  26. published_on = Column(DateTime, nullable=False)
  27.  
  28. def __repr__(self):
  29. return f"""{self.id}\t{self.title}
  30. {self.content}\n{self.author_id}\t{self.published_on}"""
  31.  
  32. engine = create_engine('sqlite:///blog.db', echo=True)
  33. Base.metadata.create_all(bind=engine)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement