views:

53

answers:

2

Suppose that I have a table like:

class Ticker(Entity):
    ticker = Field(String(7))
    tsdata = OneToMany('TimeSeriesData')
    staticdata = OneToMany('StaticData')

How would I query it so that it returns a set of Ticker.ticker?

I dig into the doc and seems like select() is the way to go. However I am not too familiar with the sqlalchemy syntax. Any help is appreciated.

ADDED: My ultimate goal is to have a set of current ticker such that, when new ticker is not in the set, it will be inserted into the database. I am just learning how to create a database and sql in general. Any thought is appreciated.

Thanks. :)

A: 

Not sure what you're after exactly but to get an array with all 'Ticker.ticker' values you would do this:

[instance.ticker for instance in Ticker.query.all()]

What you really want is probably the Elixir getting started tutorial - it's good so take a look!

UPDATE 1: Since you have a database, the best way to find out if a new potential ticker needs to be inserted or not is to query the database. This will be much faster than reading all tickers into memory and checking. To see if a value is there or not, try this:

Ticker.query.filter_by(ticker=new_ticker_value).first()

If the result is None you don't have it yet. So all together,

if Ticker.query.filter_by(ticker=new_ticker_value).first() is None:
    Ticker(ticker=new_ticker_value)
    session.commit()
Alexander Ljungberg
This is actually of what I have now. I was just thinking if there is a way to do it in elixir instead of list comprehension. If there is, is it more efficient than list comprehension.
leon
I think the second line is what I would need to use. Thank you. What is the convention on stackoverflow when i found the correct solution?
leon
If you're happy with the answer click the check mark icon on its left side to accept it. :)
Alexander Ljungberg
A: 

Use deferred columns.

Vinay Sajip