Thank you! That worked. On Saturday, August 10, 2013 5:39:31 AM UTC-4, Simon King wrote: > > > On 10 Aug 2013, at 03:42, csd...@gmail.com <javascript:> wrote: > > > This is driving me a little crazy so hopefully someone here can help. > This is my first time working with sqlalchemy (v0.8). Python is v2.7.2 and > MySQL is v14.14. > > > > The (heavily) summarized code is as follows: > > > > class Price(Base): > > __tablename__ = "prices" > > id = Column(Integer, primary_key = True) > > company_id = Column(Integer, ForeignKey('companies.id')) > > date = Column(DateTime) > > close = Column(Float) > > volume = Column(Integer) > > > > def __init__(self, date, close, volume): > > self.date = date > > self.close = close > > self.volume = volume > > > > class Company(Base): > > __tablename__ = "companies" > > id = Column(Integer, primary_key = True) > > ticker = Column(String(10)) > > company = Column(String(100)) > > prices = relationship("Price") > > def __init__(self, ticker, company): > > self.ticker = ticker > > self.company = company > > def get_prices(self): > > csv_data = get_csv() > > for row in csv_data: > > date = row[0].strip() > > date = datetime.datetime.strptime(date, '%Y-%m-%d') > > close = float(row[4]) > > volume = int(row[5]) > > prices = Price(date = date, close = close, volume = volume) > > session.add(prices) > > > > So, what the code should do is have a table of companies and a table of > daily pricing data on all companies. I want to be able to access the prices > via company.prices. Instead, when I try to do this, Python returns an empty > list []. I know that the data is getting picked up somewhere because I see > the SQL activity when I do session.commit(). I've also tried modifying the > get_prices() function by changing the prices variable to a list + append() > and then at the end of the for loop doing a session.add_all(prices), but > that didn't work either. What am I doing incorrectly? > > > > Thanks, > > > > Chris > > You haven't associated your Price instances with the Company instance. If > you looked at the database, all the company_id values would be NULL. > > If you put "self.prices.append(prices)" as the last line of your loop, it > should all work. In fact, if you do that, the "session.add" will be > unnecessary because the prices will be automatically added to the session > (assuming that the company is already in the session). > > Hope that helps, > > Simon
-- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.