On Apr 14, 11:03 am, a...@svilendobrev.com wrote:
> On Tuesday 14 April 2009 17:50:06 Dusty Phillips wrote:
>
> > On Apr 14, 10:33 am, a...@svilendobrev.com wrote:
> > > On Tuesday 14 April 2009 16:56:41 Dusty Phillips wrote:
> > > > On Apr 13, 5:16 pm, a...@svilendobrev.com wrote:
> > > > > > mapper(Document, documents, properties={
> > > > > >     'document_id': documents.c.id,   # document_id ORM
> > > > > > property In the past, I have successfully mapped these
> > > > > > properties using synonym, but this time I'm confused
> > > > > > because I'm not sure how to define the synonym to a
> > > > > > different column name. How do I change my 'document_id':
> > > > > > declaration in the mapper call to set up the above
> > > > > > descriptor when accessing the id column on the table?
>
> > > > > if u use '_document_id' in above mapper(..) would that be
> > > > > enough? no synonim, just diff.property-name
>
> > > > Wouldn't that mean that any session queries would have to be on
> > > > '_document_id' too?
>
> > > yes
>
> > > > I'd rather have the queries on the property
> > > > name -- 'document_id'.
>
> > > well, then the above and doc=synonym( _doc, ...)
>
> > so there would actually be two properties in the mapper?
>
> frankly, no idea, try

Tried (see below). It creates a document_id and a _document_id
property. It does what I want though, so I might leave it as that, but
I'd rather it wasn't allowing the session to query _document_id at all
and was only creating three properties in the mapper. Any ideas?

Dusty



In [1]: import sqlalchemy

In [2]: from sqlalchemy.orm import mapper

In [3]: from sqlalchemy import Table, Column, Integer, String,
MetaData, ForeignKey

In [4]: engine = sqlalchemy.create_engine('sqlite:///:memory:',
echo=True)

In [5]: metadata = MetaData()

In [6]: document_table = Table('documents', metadata,
   ...: Column('_rowid', Integer, primary_key=True),   ...: Column
('id', Integer),
   ...: Column('someval', String))

In [7]: class Document(object):
   ...:     @property
   ...:     def document_id(self):
   ...:         print 'getting val'
   ...:         return self._document_id
   ...:     @document_id.setter
   ...:     def document_id(self, value):
   ...:         print 'setting val'
   ...:         self._document_id = value

In [8]: m = mapper(Document, document_table, properties={
    'id': document_table.c._rowid,
    '_document_id': document_table.c.id,
    'document_id': sqlalchemy.orm.synonym('_document_id')})

In [9]: list(m.iterate_properties)
Out[9]:
[<sqlalchemy.orm.properties.ColumnProperty object at 0x1551eb0>,
 <sqlalchemy.orm.properties.ColumnProperty object at 0x1595510>,
 <sqlalchemy.orm.properties.SynonymProperty object at 0x158adf0>,
 <sqlalchemy.orm.properties.ColumnProperty object at 0x1551f70>]

In [10]: Session = sqlalchemy.orm.sessionmaker(bind=engine)

In [11]: s = Session()

In [12]: obj.document_id = 5
value is 5

In [13]: obj.someval = 'yo'

In [14]: s.add(obj)

In [15]: s.commit()
2009-04-14 11:38:16,498 INFO sqlalchemy.engine.base.Engine.0x...beb0
BEGIN
2009-04-14 11:38:16,499 INFO sqlalchemy.engine.base.Engine.0x...beb0
INSERT INTO documents (id, someval) VALUES (?, ?)
2009-04-14 11:38:16,499 INFO sqlalchemy.engine.base.Engine.0x...beb0
[5, 'yo']
2009-04-14 11:38:16,500 INFO sqlalchemy.engine.base.Engine.0x...beb0
COMMIT

In [16]: q = s.query(Document).filter_by(document_id=5)

In [17]: r = q.all()
2009-04-14 11:39:20,720 INFO sqlalchemy.engine.base.Engine.0x...beb0
BEGIN
2009-04-14 11:39:20,721 INFO sqlalchemy.engine.base.Engine.0x...beb0
SELECT documents.id AS documents_id, documents._rowid AS
documents__rowid, documents.someval AS documents_someval
FROM documents
WHERE documents.id = ?
2009-04-14 11:39:20,722 INFO sqlalchemy.engine.base.Engine.0x...beb0
[5]

In [18]: q = s.query(Document).filter_by(_document_id=5)

In [19]: q.all()[0].id
2009-04-14 11:41:35,741 INFO sqlalchemy.engine.base.Engine.0x...beb0
SELECT documents.id AS documents_id, documents._rowid AS
documents__rowid, documents.someval AS documents_someval
FROM documents
WHERE documents.id = ?
2009-04-14 11:41:35,752 INFO sqlalchemy.engine.base.Engine.0x...beb0
[5]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to