Jaigus
Jaigus

Reputation: 1502

Are models always necessary to represent a particular db table?

I have a database that is practically isomorphic to the model classes that it almost seems like a waste of time to create them. I'm using sqlalchemy core (not the orm), and I created gateway classes that look something like this example:

class BanGate(Gateway):

    def __init__(self):
            super(BanGate, self).__init__('ban')

    def ban_user(self, user, group, exp):
            data = dict(user_id=user, group_id=group, expires=exp)
            self._table.insert(values=data).execute()

    def unban_user(self, user, group):
            cond = and_(self._table.c.user_id == user, self._table.c.group_id == group)
            self._table.delete(whereclause=cond).execute()

class ForumGate(Gateway):

    def __init__(self):
            super(ForumGate, self).__init__('forum')

    def create_forum(self, group, user, title, desc):
            data = dict(group_id=group, creator=user, name=title, description=desc)
            self._table.insert(values=data).execute()

    def delete_forum(self, forum):
            stmt = (self._table.c.forum_id == forum)
            self._table.delete(whereclause=stmt).execute()

Creating a "ban" or "forum" class to hold on to state and save it using these gateways seems like a waste of time. For the controllers, I'm thinking about simply taking data from the request object and directly using these gateway objects, is this advisable? Most of the data for pages don't need to be manipulated, just saved and/or read, so the use of models classes don't seem necessary.

Also, the table classes used inside the gateways use the autoload feature, so I didn't define the database schema.

So basically how do I go about persisting data? Should I create a model class that gets passed into these gateway objects, or should I just simply pass the gateway objects data directly from the request objects?

(Also, by "gatway", I am referring to http://martinfowler.com/eaaCatalog/tableDataGateway.html)

Upvotes: 0

Views: 402

Answers (1)

zzzeek
zzzeek

Reputation: 75127

Well in this case, you're reinventing an ORM by having to hand-code all those insert()/update() constructs etc, which is a lot more work than just using the persistence and querying facilities that are there. If the "waste of time" is having the classes defined in code, there's no need for that if you want to use reflection, use a tool like SQLSoup or just roll something similar, or perhaps DeferredReflection. In any ORM configuration, Python classes can be created on the fly using type(), so the ORM can be leveraged dynamically.

Upvotes: 2

Related Questions