Dynamically setting __tablename__ for sharding in SQLAlchemy?

后端 未结 6 1541
我寻月下人不归
我寻月下人不归 2020-12-28 17:11

In order to handle a growing database table, we are sharding on table name. So we could have database tables that are named like this:

table_md5one
table_md5         


        
6条回答
  •  天涯浪人
    2020-12-28 17:43

    In Augmenting the Base you find a way of using a custom Base class that can, for example, calculate the __tablename__ attribure dynamically:

    class Base(object):
        @declared_attr
        def __tablename__(cls):
            return cls.__name__.lower()
    

    The only problem here is that I don't know where your hash comes from, but this should give a good starting point.

    If you require this algorithm not for all your tables but only for one you could just use the declared_attr on the table you are interested in sharding.

提交回复
热议问题