I'm dealing with timeseries data, and I'm trying to avoid having a many-to-one-relashionship on every row of a single record table where all records are mixed together (since the series all have different number of columns).
Instead I want to split the records between multiple tables to keep individual data series apart.
I have a lot of these tables:
# series 1 record tables:
CREATE TABLE records_1 (
id BIGSERIAL PRIMARY KEY,
level DOUBLE PRECISION
#... more columns
);
CREATE TABLE records_2 (
id BIGSERIAL PRIMARY KEY,
level DOUBLE PRECISION
#... more columns
);
CREATE TABLE records_34 (
id BIGSERIAL PRIMARY KEY,
level DOUBLE PRECISION
#... more columns
);
# series 2 record tables
CREATE TABLE records_101 (
id BIGSERIAL PRIMARY KEY,
level DOUBLE PRECISION,
height DOUBLE PRECISION
#... more columns
);
CREATE TABLE records_102 (
id BIGSERIAL PRIMARY KEY,
level DOUBLE PRECISION,
height DOUBLE PRECISION
#... more columns
);
And then I have a class with a string representing the table names associated with that class:
class Serie(db.Model):
id = db.Column(db.Integer, primary_key=True)
record_tables = db.Column(db.String(255))
Example:
s1 = Serie()
s1.record_tables = 'records_1, records_2, records_34'
s2 = Serie()
s2.record_tables = 'records_101, records_102'
I'd like to be able to select all records associated with a given serie, based on the record_tables string. Splitting and stripping it of whitespace is easy, but how would I build the query dynamically?
Something like "for each table in record_tables_list, select all records from table and return all records from all tables in list" but in one sql query instead of appending results to a list in python (which I imagine is slower).
I guess I should use UNION, as I have recreated such a query manually in SQL. But how do do it with an unknown number of table names in Serie.record_tables?
Update:
Ok, so a raw query seems to work while joining the list of sanitized record_tables (it depends on user input (my own) so I have to clean it up to remove typos and such):
tables = ', '.join([x.strip() for x in record_tables.split(',')])
raw_query = 'SELECT * FROM ' + tables
results = db.engine.execute(raw_query)
for row in raw_query:
print row # (1L, 123.0, 1L, 456.0)
Edit: no this doesn't work. hm.