Materialized Path for SQLAlchemy & Declarative Base

134 views
Skip to first unread message

AF

unread,
Aug 6, 2009, 6:54:36 PM8/6/09
to sqlalchemy
Hello all,

Has anyone here used the "sqlamp: Materialized Path for SQLAlchemy"
library?

I am wondering:

1) Does it seem to work well?

2) Did you use it with Declarative Base, and if so, how did you
configure it?

Thank you,
:)

allen.fowler

unread,
Aug 7, 2009, 10:34:45 AM8/7/09
to sqlalchemy
Anybody?

Specifically, I am wondering about how adapt the sample code at:

http://sqlamp.angri.ru/#quickstart

... so that it works with declarative base.


Thank you,
:)

werner

unread,
Aug 7, 2009, 12:36:39 PM8/7/09
to sqlal...@googlegroups.com
Allen,

I haven't used this library and I am no SA expert so take the following
with a grain (or two) of salt.

I would translate this:

class Node(object):
mp = sqlamp.MPManager(
node_table, node_table.c.id, node_table.c.parent_id
)

To:
class Node(Base):
__table__ = sa.Table(u'node', metadata,
sa.Column(u'id', sa.Integer(), sa.Sequence('gen_sample_id'), primary_key=True, nullable=False),
sa.Column(u'parent_id', sa.Integer(), sa.ForeignKey(u'node.id')),
...
)

mp = sqlamp.MPManager(
__table__, __table__.c.id, __table__.c.parent_id)


Werner

allen.fowler

unread,
Aug 9, 2009, 5:42:24 PM8/9/09
to sqlalchemy
Werner,
Thank you...

Though, that does not look at all like typical Declarative Base code
I've seen / been using. Why the explicit assignment to __table__?

Further, where does the "extension=[Node.mp.mapper_extension]" binding
happen?

Thank you again,
AF

Anton Gritsay

unread,
Aug 13, 2009, 2:37:49 PM8/13/09
to sqlalchemy
Hi, Allen!

You can use something like this (yeah, I know that it isn't
declarative in any way):

class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_id = Column(ForeignKey('node.id'))

parent = relation("Node", remote_side=[id])

mp_path = Column(sqlamp.PathField())
mp_depth = Column(sqlamp.DepthField())
mp_tree_id = Column(sqlamp.TreeIdField())

Node.mp = sqlamp.MPManager(
Node.__table__,
pk_field='id',
parent_id_field='parent_id',
path_field_name='mp_path',
depth_field_name='mp_depth',
tree_id_field_name='mp_tree_id'
)
Node.__mapper__.extension.append(Node.mp.mapper_extension)

Note that you need to define mp_* colums. You doesn't need it if you
create MPManager *before* creating mapper.

On the other hand you can use this hack (but I wouldn't recomend it):

class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_id = Column(ForeignKey('node.id'))

parent = relation("Node", remote_side=[id])

Node.mp = sqlamp.MPManager(
Node.__table__,
pk_field='id',
parent_id_field='parent_id'
)
Node.__mapper__.extension.append(Node.mp.mapper_extension)
Node.__mapper__._configure_properties()

allen.fowler:

allen.fowler

unread,
Aug 13, 2009, 9:23:19 PM8/13/09
to sqlalchemy


On Aug 13, 2:37 pm, Anton Gritsay <gene...@angri.ru> wrote:
> Hi, Allen!
>
> You can use something like this (yeah, I know that it isn't
> declarative in any way):
>
>     class Node(Base):
>         __tablename__ = 'node'
>         id = Column(Integer, primary_key=True)
>         parent_id = Column(ForeignKey('node.id'))
>
>         parent = relation("Node", remote_side=[id])
>
>         mp_path = Column(sqlamp.PathField())
>         mp_depth = Column(sqlamp.DepthField())
>         mp_tree_id = Column(sqlamp.TreeIdField())
>
>     Node.mp = sqlamp.MPManager(
>         Node.__table__,
>         pk_field='id',
>         parent_id_field='parent_id',
>         path_field_name='mp_path',
>         depth_field_name='mp_depth',
>         tree_id_field_name='mp_tree_id'
>     )
>     Node.__mapper__.extension.append(Node.mp.mapper_extension)
>
> Note that you need to define mp_* colums. You doesn't need it if you
> create MPManager *before* creating mapper.
>



Anton,


Thank you for the help. And the excellent library. :)

I will try this code out as soon as I can.

Michael B:
Is this the only way to register such an extension with declarative
base? Can a simpler method be devised?

-- AF

Michael Bayer

unread,
Aug 13, 2009, 9:36:24 PM8/13/09
to sqlal...@googlegroups.com
err well if you wanted to cut down on verbosity possibly a custom
metaclass that descends from DeclarativeMeta, or sqlamp can build some
kind of "deferred" system bywhich the mapper extension figures out
what it needs from the mapper passed to it...since the extension is
dependent on the table object nothing spectacular is apparent otherwise.




>
>
> -- AF
> >

Reply all
Reply to author
Forward
0 new messages