Which line is triggering that error?

Here's a working example:

import sqlalchemy as sa
import sqlalchemy.orm as saorm
from sqlalchemy.ext.automap import automap_base

Base = automap_base()


class Host(Base):
    __tablename__ = 'hosts'

    _mac_address = sa.Column('mac_address', sa.Integer)

    @property
    def mac_address(self):
        return '%012x' % self._mac_address

    @mac_address.setter
    def mac_address(self, value):
        self._mac_address = int(value, 16)


def main():
    engine = sa.create_engine('sqlite://')

    engine.execute("""
        CREATE TABLE hosts(
          id INTEGER PRIMARY KEY,
          mac_address INTEGER
        )
    """)

    Base.prepare(engine, reflect=True)

    session = saorm.sessionmaker(bind=engine)()

    host = Host(mac_address='001122334455')
    session.add(host)
    session.commit()

    host = session.query(Host).one()
    print host.mac_address
    print host._mac_address

if __name__ == '__main__':
    main()


On Tue, Aug 30, 2016 at 12:09 AM, Robert Minsk <rmi...@gmail.com> wrote:
> When I try to do that I get
>
>  File
> "/home/CORP/robert.minsk/venvs/lfw/lib/python2.7/site-packages/sqlalchemy/util/_collections.py",
> line 212, in __getattr__
>
>     raise AttributeError(key)
>
> AttributeError: hosts
>
>
> On Fri, Aug 26, 2016 at 1:33 AM, Simon King <si...@simonking.org.uk> wrote:
>>
>> On Fri, Aug 26, 2016 at 2:24 AM, Robert Minsk <rmi...@gmail.com> wrote:
>> > I am using automap_base to reflect a table.
>> >
>> > metadata = MetaData()
>> > metadata.reflect(bind=engine, only=['hosts'])
>> > automap = automap_base(metadata=metadata)
>> > automap.prepare()
>> >
>> > Hosts = automap.classes.hosts
>> >
>> > The mac_address column in Hosts is  binary and would like to to replace
>> > the
>> > mac_address with some sort of getter/setter to decode/encode the
>> > address.  I
>> > have not found an example of doing this with an automapped table.  Is
>> > there
>> > a easy way to do this?
>>
>> You can override specific columns on an automapped table by
>> pre-declaring the class:
>>
>>
>> http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#specifying-classes-explicitly
>>
>> I haven't tried it, but I would assume that you can use any of the
>> normal facilities for modifying column behaviour at that point. For
>> example, I think you could do something like this:
>>
>> class Hosts(automap):
>>     __tablename__ = 'hosts'
>>     _mac_address = Column('mac_address', BINARY)
>>     @property
>>     def mac_address(self):
>>         return decode_mac_address(self._mac_address)
>>
>>     @mac_address.setter
>>     def mac_address(self, value):
>>         self._mac_address = encode_mac_address(value)
>>
>> If you want to be able to query using the decoded values, you could
>> also try hybrid properties.
>>
>> Hope that helps,
>>
>> Simon
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "sqlalchemy" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/sqlalchemy/uxto-cNocFw/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> sqlalchemy+unsubscr...@googlegroups.com.
>> To post to this group, send email to sqlalchemy@googlegroups.com.
>> Visit this group at https://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to