On Fri, May 23, 2008 at 11:19 PM, Venkatesh <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
>  I am trying to create a base class for my application which inherits
> from Entity like this:
>
> class appBase(Entity):
>    def __str__(self):
>        outStr = ''
>        attrList = list(self.__dict__.keys())
>        attrList.sort()
>
>        for attrName in attrList:
>            outStr = outStr + attrName
>            value = getattr(self, attrName)
>
>            if value.__class__.__name__ == 'int':
>                outStr = outStr + '->' + str(value) + '\n'
>                attrName = Field(Integer)
>
>            if value.__class__.__name__ == 'str':
>                outStr = outStr + '->' + str(value) + '\n'
>                attrName = Field(String(100))
>
>        return outStr
>
>
> class User(appBase):
>    using_options(tablename='SqlUser')
>
>    credentials = OneToMany('Credential')

Well, what you are trying to do is provide a different base class than
Entity. The behavior you are observing is normal even if can be a
little confusing. The thing is that any any class inheriting from
Entity will create it's own table. appBase being no exception. The
other part of your confusion is that the inheritance "mode" is "single
table inheritance" by default. Basically, it means that the data for
children classes are put in the table as their parent (and won't
create a table for themselves). In your case, the User class data will
end up in the appBase table.

That said, you have two options to achieve what you want:
a) provide a new base class (which can be used instead of the provided
Entity class):

class appBase(object):
    __metaclass__ = EntityMeta

    def __str__(self):
        [...]

class User(appBase):
    [...]

Note that in this case you'll lose all default methods provided by the
Entity class (see the code for details), but nothing prevents you to
copy-paste the code of the methods you want in your entity.

b) use a mixin class:

class StrMixin(object):
    def __str__(self):
        [...]

class User(Entity, StrMixin):
    [...]

> The reason I want to do it is to create databases using the
> Introspection power of Python.
>
> I can see the object getting created in memory, but when I try to
> create the database using setup_all(True), I can only see that appBase
> table is getting created.
>
> Is it that to create a table, i **have** to inherit from Entity?
>
> Is it possible to create a table in the way that I have described?

Hope it helps,

-- 
Gaƫtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to