Re: [Project Camelot] Model inheritance

42 views
Skip to first unread message

Erik Janssens

unread,
Apr 12, 2013, 2:59:25 PM4/12/13
to project...@googlegroups.com
Hello,

in your Engineer class, the __tablename__ needs to be set to None,
otherwise Camelot will assign a default tablename, and SQLAlchemy
will assume multi table inheritance, and thus requires a foreign key
relationship.

I've added a unittest on this case, see this commit :

https://bitbucket.org/conceptive/camelot/commits/f58643035acda07f9e6914f8b3a69f522b2b9d41

for a concrete example.

Cheers,

Erik



On Thu, Apr 11, 2013 at 8:50 PM, Roland Szabo <rol...@gmail.com> wrote:
How do you do single table inheritance for models in Camelot? I tried doing something similar to how you do it in SQLAlchemy but it gives an error.

class Person(Entity):
    __tablename__ = 'people'
    id = Column(sqlalchemy.Integer, primary_key=True)
    discriminator = Column('type', sqlalchemy.String(50))
    __mapper_args__ = {'polymorphic_on': discriminator}

    class Admin(EntityAdmin):
        verbose_name = 'Person'
        list_display = ['id','discriminator']

class Engineer(Person):
    __mapper_args__ = {'polymorphic_identity': 'engineer'}
    primary_language = Column(sqlalchemy.String(50))

    class Admin(EntityAdmin):
        verbose_name = 'HR'
        list_display = ['id','discriminator']
This gives the following error:

Traceback (most recent call last):
    File "D:/Programming/test/main.py", line 41, in <module>
      start_application()
    File "D:/Programming/test/main.py", line 37, in start_application
      from testing.application_admin import MyApplicationAdmin
    File "D:\Programming\test\testing\application_admin.py", line 6, in <module>
      from testing.model import Engineer, Person
    File "D:\Programming\test\testing\model.py", line 17, in <module>
      class Engineer(Person):
    File "C:\Python27\lib\site-packages\camelot\core\orm.py", line 334, in __init__
      super( EntityMeta, cls ).__init__( classname, bases, dict_ )
    File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py", line 1343, in __init__
      _as_declarative(cls, classname, cls.__dict__)
    File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py", line 1336, in _as_declarative
      **mapper_args)
    File "C:\Python27\lib\site-packages\sqlalchemy\orm\__init__.py", line 1129, in mapper
      return Mapper(class_, local_table, *args, **params)
    File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 197, in __init__
      self._configure_inheritance()
    File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 473, in _configure_inheritance
      self.local_table)
    File "C:\Python27\lib\site-packages\sqlalchemy\sql\util.py", line 303, in join_condition
      "between '%s' and '%s'.%s" % (a.description, b.description, hint))
  sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'people' and 'engineer'.

--
--
You received this message because you are subscribed to the "Project Camelot" group.
Visit www.python-camelot.com for more information
 
To post to this group, send email to project...@googlegroups.com
To unsubscribe from this group, send email to
project-camel...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/project-camelot?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Project Camelot" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-camel...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Roland Szabo

unread,
Apr 12, 2013, 4:21:25 PM4/12/13
to project...@googlegroups.com, erik.j...@conceptive.be
Thanks! That's what I was looking for!

Camelot is pretty awesome!
Reply all
Reply to author
Forward
0 new messages