Z
Z
zasqer2014-09-06 12:20:39
Python
zasqer, 2014-09-06 12:20:39

How to add a foreign key to an existing table in SQLAlchemy?

There are such classes where several indicators (Indicator) belong to one report (Report):

class Report(Base):
    __tablename__ = 'report'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    comment = Column(String)
    regularity = Column(Integer)
    theme = Column(Integer)


class Indicator(Base):
    __tablename__ = 'indicator'
    date = Column(String, primary_key=True)
    value = Column(String)
    report = Column(Integer, primary_key=True)

The database already has data
The report field in the Indicator table contains Report.id
The date, report fields in the Indicator table are a composite primary key
How to add a many-to-one relationship (many indicators to one report) if the database already exists and it already has data?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2014-09-06
@zasqer

Migrations
https://pypi.python.org/pypi/sqlalchemy-migrate

S
s1dney, 2014-09-06
@s1dney

There is such a "Mega tutorial Flask", it was translated on Habré, there is an article about databases with an example of sqlalchemy migration (sqlite)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question