Dynamic Table Creation and ORM mapping in SqlAlchemy

前端 未结 7 1876
忘了有多久
忘了有多久 2020-12-23 19:18

I\'m fairly new to using relational databases, so I prefer using a good ORM to simplify things. I spent time evaluating different Python ORMs and I think SQLAlchemy is what

7条回答
  •  渐次进展
    2020-12-23 19:59

    Maybe look at SQLSoup, which is layer over SQLAlchemy.

    You can also create the tables using plain SQL, and to dynamically map, use these libraries if they already don't have create table function.

    Or alternatively create a dynamic class and map it:

    tableClass = type(str(table.fullname), (BaseTable.BaseTable,), {})
    mapper(tableClass, table)
    

    where BaseTable can be any Python class which you want all your table classes to inherit from, e.g. such Base class may have some utility or common methods, e.g. basic CRUD methods:

    class BaseTable(object): pass
    

    Otherwise you need not pass any bases to type(...).

提交回复
热议问题