I'm trying to figure out how to use SQLAlchemy for domain models for a
"sharded" table. I put "sharded" in quotes because it is almost certainly
not the correct technical term for what I'm trying to do. Let me explain.
I want to create tables with the name "foos_YYYY_MM" where YYYY is the year
and MM is the month of the last update. Let's say we call the model Foo,
and it has a "updated_date" column. So say we have a Foo stored in the
table `foos_2016_01`, then we go get it and update it, it might need to go
in the table `foos_2016_02` now. Does that make sense?
After googling and playing around with things a bit I have this code:
class Foo(object):
def __init__(self, ...)
self.updated_date = ...
self.col1 = ...
...
[...]
def get_foo(updated_date):
model =
Table('foo_{date}'.format(date=updated_date.strftime('%Y_%m')),
metadata,
Column(...),
Column(...),
Column(...))
clear_mappers()
m = mapper(Foo, model)
m.mapped_table.create(engine, checkfirst=True)
return Foo
But this is almost certainly wrong because it relies on `clear_mappers()`
being called, otherwise you get the error "already has a primary mapper
defined." the second time you call the `get_foo()` function. I'd also like
to try and use a declarative syntax instead of this classical syntax if
possible, but I couldn't figure out how to get it to work with a
declarative base.
Sorry to be kind of vague here, I'm really not sure what the correct
terminology would be for this kind of concept. I feel like there must be a
way to do this using SQLAlchemy, but I am struggling to figure out exactly
how.
Thanks in advance for any help,
Amy
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.