Sajad
Sajad

Reputation: 524

PonyORM on an existing table

Is there any way to use PonyORM for an existing database?

I have a table Test in my db and I want to define an Entity Test to connect to that table, I tried to use _table_ = 'Test' and __table_name__ = 'Test' like:

class Test(db.Entity):
    __table_name__ = 'Test'

This allowed me to execute queries on table Test, but I can't access table columns via Entity attributes (I guess because it does not load them from db automatically).

For example, when I try to create a new instance of Test, I get:

Test(my_attr = 'Hello')

>>> TypeError: Unknown attribute 'my_attr'

I'm working on a Database First designed project and the database is changing constantly, so keep redefining columns in Entities seems like a bad solution.

Upvotes: 0

Views: 498

Answers (1)

Muhammed YILMAZ
Muhammed YILMAZ

Reputation: 81

You must write columns to class. This way it can correctly link your database table to the class.

For example

class Test(db.Entity):
    __table_name__ = 'Test'

    my_attr = Optional(str)

Upvotes: 0

Related Questions