Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from sqlalchemy import Column, ForeignKey, Integer, String
  4. from sqlalchemy.ext.declarative import declarative_base
  5. from sqlalchemy.orm import relationship
  6. from sqlalchemy import create_engine
  7.  
  8. Base = declarative_base()
  9.  
  10.  
  11. class User(Base):
  12. """Create a Users table"""
  13. __tablename__ = 'user'
  14.  
  15. id = Column(Integer, primary_key=True)
  16. name = Column(String(250), nullable=False)
  17. email = Column(String(250), nullable=False)
  18. picture = Column(String(250))
  19. placeTypes = relationship('PlaceType', backref="user", passive_deletes=True)
  20. places = relationship('Place', backref="user", passive_deletes=True)
  21.  
  22.  
  23. class PlaceType(Base):
  24. """Create a PlaceType table"""
  25. __tablename__ = 'placeType'
  26.  
  27. id = Column(Integer, primary_key=True)
  28. name = Column(String(250), nullable=False)
  29. user_id = Column(Integer, ForeignKey('user.id', ondelete='CASCADE'))
  30. places = relationship('Place', backref="placeType", passive_deletes=True)
  31.  
  32. # Set up the related JSON API endpoints
  33. @property
  34. def serialize(self):
  35. """Return object data in an easily serializeable format"""
  36. return {
  37. 'name': self.name,
  38. 'id': self.id
  39. }
  40.  
  41.  
  42. class Place(Base):
  43. """Createa a Place table"""
  44. __tablename__ = 'place'
  45.  
  46. id = Column(Integer, primary_key=True)
  47. name = Column(String(80), nullable=False)
  48. description = Column(String(850))
  49. location = Column(String(250), nullable=False)
  50. price = Column(String(10))
  51. link = Column(String(300))
  52. placeType_id = Column(Integer, ForeignKey('placeType.id', ondelete='CASCADE'))
  53. user_id = Column(Integer, ForeignKey('user.id', ondelete='CASCADE'))
  54.  
  55. # Set up the related JSON API endpoints
  56. @property
  57. def serialize(self):
  58. """Return object data in an easily serializeable format"""
  59. return {
  60. 'name': self.name,
  61. 'id': self.id,
  62. 'description': self.description,
  63. 'location': self.location,
  64. 'price': self.price,
  65. 'link': self.link
  66. }
  67.  
  68.  
  69. # Create an SQLite database called nuevomexico.db
  70. engine = create_engine('postgresql://catalog:INSERT_PASSWORD_HERE@localhost/catalog')
  71.  
  72. Base.metadata.create_all(engine)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement