lundi 27 juin 2016

sqlalchemy set atrribute result in None


Im trying to figure out how relationships work in sqlalchemy. Im trying to set up a one-to-one relation between raw- and analyzedData, with analyzedData having an extra column pointing back to the rawData. But now when trying to set this attribute, it result in the rawData_id being none. Can someone point out what I'm doing wrong. I also don't get a lot wiser from the documentation.

Below I have put the simplified code:

class RawData(Base):
        """
            Will map to the rawData. 

            Paratemers: date(Date), the date 
            Paratemers: symbol(String), the symbol 
            ToDo: 
            - Create the relation between rawData and Analyzed Data
        """
        __tablename__ = 'rawData'
        version = '0.0.1'

        id = Column(Integer, primary_key=True)
        date = Column(Date)
        symbol = Column(String(50))

        def __init__(self, date, symbol):
            self.date = date
            self.symbol = symbol

    class AnalyzedMarketData(Base):
        """
            Will map to the Analyzed Data table in the DB

            Paratemers: date(Date), the date of the analyzed-object
            Paratemers: symbol(String),
            Paratemers: isBull(Boolean),
        """
        __tablename__ = 'analyzedData'
        version = '0.0.1'

        id = Column(Integer, primary_key=True)
        date = Column(Date)
        symbol = Column(String(50))
        isBull = Column(String(50))

        # Object Relation to Rawdata
        rawData_id = Column(Integer, ForeignKey("rawData.id"))
        rawData = relationship("RawData", backref='rawData')

        def __init__(self, date, symbol, isBull):
            self.date = date
            self.symbol = symbol
            self.isBull  = isBull

        def __repr__(self):
            return "<AnalyzedMarketData: %r %r  >" % (self.symbol, self.isBull)



    def main():
        for rawDataResult in session.query(RawData).filter_by(date=today): # for result in queryResults:
            # pass all the results to the analyser
            analyzedResult = analyzer.analyzeRawData(rawDataResult) # analyzedResult = analyzeRD(result)
            rpd = RawData(2, date.today(), "blaSymbol")
            analyzedResult.rawData = rpd
            print(analyzedResult.rawData)

            # this is the resulting query 
            INFO sqlalchemy.engine.base.Engine INSERT INTO `analyzedMarketData` (date, symbol, `isBull` raw_id) VALUES (%s, %s, %s
            INFO sqlalchemy.engine.base.Engine (datetime.date(2016, 6, 26), 'blaSymbol' None)

Aucun commentaire:

Enregistrer un commentaire