Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM for python that does not require we to define all properties for classes for database tables

I have access to a large database system. I would like to talk with it in an efficient manner.

Are there ORM framework like SQLAlchemy (I know SQLAlchemy) that does not require we to define all properties for classes for each database tables?

Because the database is already there, my aim is to avoid creating properties for classes.

like image 277
Junaid Avatar asked May 13 '26 20:05

Junaid


1 Answers

Using SQLAlchemy's introspection features you can easily enough have a metaclass that gives you a new mapped class given a table name. The class still has to be defined, but you don't have to define it.

def introspect(tablename, *mapper_args, **mapper_kwargs):
  u'given a table name and optional mapper arguments return an ORM class'
  global metadata  # or pass it in, or use OO, whatever
  global engine    # or pass it in, or use OO, whatever
  table = sqlalchemy.Table(tablename, metadata,
      autoload=True, autoload_with=engine)
  class DynamicClass: pass  # you can provide nice __init__, __str__ methods
  return sqlalchemy.orm.mapper(DynamicClass, table,
      *mapper_args, **mapper_kwargs)

Hibernate has a similar introspection feature, but it generates Java source files, and therefore is a build-time, not run-time, operation.

like image 53
wberry Avatar answered May 15 '26 10:05

wberry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!