如何自动填充表中定义SQL Alcehmy中另外两个表之间关系的日期?

时间:2016-04-20 02:58:35

标签: python sqlite sqlalchemy flask-sqlalchemy

所以,我正在使用sqlite3数据库在python Flask中编写一个webapp。我正在使用SQLAlchemy。作为我的数据库的一部分,我有这三个表。在这里,表DisasterEvent与Location Table有很多关系(灾难事件发生在许多地方,并且在该特定位置可能发生了许多灾难。当我在DisasterEvent和Location表中插入数据时,有没有办法自动填充关联表或我应该自己填充它吗?即使你们没有经验丰富的sql炼金术经验,我也很想听听它是如何完成的?

class DisasterEvent(Base):
    '''Disaster Event Table'''

    __tablename__ = 'DisasterEvent'

    id = Column(Integer, primary_key = True, autoincrement = True)
    title = Column(String(80), nullable = False)
    glide = Column(String, nullable = False)                     
    status = Column(Enum('current', 'alert', 'past'))
    description = Column(String)
    date_occured = Column(String(11), nullable = False)
    date_ended = Column(String(11))
    disaster_type = Column(String(120), nullable  = False)

    location = relationship("Location", secondary = association_table,
                        back_populates = 'disaster_event')
class Location(Base):
    '''Disaster Location Table'''

    __tablename__ = 'Location'

    id = Column(Integer, primary_key = True)
    country = Column(String)
    state = Column(String)
    city = Column(String)
    lat = Column(Float)
    lon = Column(Float)

    disaster_event = relationship("DisasterEvent", secondary = association_table,
                            back_populates = 'location')

#this is the association table that joins the two above tables
association_table = Table('association', Base.metadata,
    Column('event_id', Integer, ForeignKey('DisasterEvent.id')),
    Column('location_id', Integer, ForeignKey('Location.id')))

0 个答案:

没有答案