Oh. This isn't the same example that I was talking about. From the example 
in the doc, you are allowed to have the column and the synonym have the 
same name, but your example has _job_status for the column and job_status 
for the synonym. when I do it your way, inspect shows _job_status as an attr


>>> inspect(mc).attrs.keys()
['job_status', '_job_status', 'id']

The way listed in the docs had me believing that you could change these 
lines from your example:
    _job_status = Column("job_status", String(50)) 
    job_status = synonym("_job_status") 

to 

    job_status = Column("job_status", String(50)) 
    job_status = synonym("_job_status") 


no leading underscore. When doing it this way, the job_status column is 
never detected or created. I just realized that the example I'm following 
is literally the section for "map_column", so does that mean that 
functionality isn't supported anymore?

here's the link to the section I'm speaking about specifically:

http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html#sqlalchemy.orm.synonym.params.map_column

The example is what I was copying because the description of what it does 
is what I was hoping to achieve.




On Wednesday, January 24, 2018 at 5:45:51 PM UTC-8, Mike Bayer wrote:

> On Wed, Jan 24, 2018 at 7:25 PM, dykang <dyk...@gmail.com <javascript:>> 
> wrote: 
> > Thank you for the tip, but If I remove map_column=True from the example, 
> > then inspect returns the correct listing, but when I load an object from 
> the 
> > db, neither job_status nor _job_status are accessible on the object. It 
> > seems to work on assignment, but not on load from db. Is there something 
> > more I have to do? 
>
> here is a complete example: 
>
> from sqlalchemy import * 
> from sqlalchemy.orm import * 
> from sqlalchemy.ext.declarative import declarative_base 
>
> Base = declarative_base() 
>
>
> class MyClass(Base): 
>     __tablename__ = 'my_table' 
>     id = Column(Integer, primary_key=True) 
>     _job_status = Column("job_status", String(50)) 
>     job_status = synonym("_job_status") 
>
> e = create_engine("sqlite://", echo=True) 
> Base.metadata.create_all(e) 
>
> s = Session(e) 
> mc = MyClass(job_status="some status") 
> s.add(mc) 
> s.commit() 
> s.close() 
>
> mc = s.query(MyClass).first() 
> assert mc.job_status == "some status" 
>
>
>
>
> > 
> >>>> a = session.query(myclass.MyClass).get(1) 
> >>>> a 
> > <MyClass(id=1)> 
> >>>> a.id 
> > 1 
> >>>> a.__mapper__.columns 
> > <sqlalchemy.util._collections.OrderedProperties object at 
> 0x7fa91f92c0c0> 
> >>>> a.__mapper__.columns.keys() 
> > ['id'] 
> >>>> a.job_status 
> > Traceback (most recent call last): 
> >   File "<stdin>", line 1, in <module> 
> >   File 
> > 
> "virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py",
>  
>
> > line 298, in __get__ 
> >     return self.descriptor.__get__(instance, owner) 
> >   File 
> > 
> "virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/orm/descriptor_props.py",
>  
>
> > line 58, in fget 
> >     return getattr(obj, self.name) 
> > AttributeError: 'MyClass' object has no attribute '_job_status' 
> > 
> > 
> > On Wednesday, January 24, 2018 at 2:33:39 PM UTC-8, Mike Bayer wrote: 
> >> 
> >> remove map_column=True, will update the docs now. 
> >> 
> >> On Wed, Jan 24, 2018 at 5:01 PM, dykang <dyk...@gmail.com> wrote: 
> >> > I was trying to use a synonym to map a column through a setter/getter 
> >> > (with 
> >> > use of descriptor kwarg). I'm not sure if I'm doing this right, but I 
> >> > tried 
> >> > to copy/paste the example out of the documentation, and it didn't 
> work. 
> >> > Could someone point out what I'm doing wrong or point me at a better 
> way 
> >> > to 
> >> > do what I want? I've tried with 1.1.12 and 1.2.1 with python 2.7. 
> >> > 
> >> > this is the entirety of what I'm trying to do: 
> >> > ------------------------------------------------------ 
> >> > 
> >> > from sqlalchemy.ext import declarative 
> >> > Base = declarative.declarative_base() 
> >> > from sqlalchemy import * 
> >> > from sqlalchemy.orm import synonym 
> >> > 
> >> > class MyClass(Base): 
> >> >     __tablename__ = 'my_table' 
> >> >     id = Column(Integer, primary_key=True) 
> >> >     job_status = Column(String(50)) 
> >> >     job_status = synonym("_job_status", map_column=True) 
> >> > 
> >> > ------------------------------------------------------ 
> >> > 
> >> > This fails because the synonym won't compile. It appears that the 
> >> > job_status 
> >> > Column value has been overridden by the synonym before it's had a 
> chance 
> >> > to 
> >> > create the synonym to the original.  It seems I can get around this 
> by 
> >> > not 
> >> > using the map_column value and just renaming the column to 
> _job_status, 
> >> > but 
> >> > I was hoping that using sqlalchemy.inspect(row_instance).attrs would 
> >> > only 
> >> > return job_status, and not _job_status (I'm not even sure if this is 
> >> > true, 
> >> > it was just a guess on how it might work and what I wanted to happen) 
> >> > 
> >> > Traceback (most recent call last): 
> >> >   File "<stdin>", line 1, in <module> 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py",
>  
>
> >> > line 64, in __init__ 
> >> >     _as_declarative(cls, classname, cls.__dict__) 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py",
>  
>
> >> > line 88, in _as_declarative 
> >> >     _MapperConfig.setup_mapping(cls, classname, dict_) 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py",
>  
>
> >> > line 116, in setup_mapping 
> >> >     cfg_cls(cls_, classname, dict_) 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py",
>  
>
> >> > line 148, in __init__ 
> >> >     self._early_mapping() 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py",
>  
>
> >> > line 151, in _early_mapping 
> >> >     self.map() 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py",
>  
>
> >> > line 576, in map 
> >> >     **self.mapper_args 
> >> >   File "<string>", line 2, in mapper 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
>  
>
> >> > line 692, in __init__ 
> >> >     self._configure_properties() 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
>  
>
> >> > line 1383, in _configure_properties 
> >> >     self._configure_property(key, prop, False) 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
>  
>
> >> > line 1677, in _configure_property 
> >> >     prop.set_parent(self, init) 
> >> >   File 
> >> > 
> >> > 
> "/virtualenv/workspace/local/lib/python2.7/site-packages/sqlalchemy/orm/descriptor_props.py",
>  
>
> >> > line 626, in set_parent 
> >> >     % (self.name, parent.mapped_table.description, self.key)) 
> >> >  sqlalchemy.exc.ArgumentError: Can't compile synonym '_job_status': 
> no 
> >> > column on table 'my_table' named 'job_status' 
> >> > 
> >> > -- 
> >> > SQLAlchemy - 
> >> > The Python SQL Toolkit and Object Relational Mapper 
> >> > 
> >> > http://www.sqlalchemy.org/ 
> >> > 
> >> > To post example code, please provide an MCVE: Minimal, Complete, and 
> >> > Verifiable Example. See http://stackoverflow.com/help/mcve for a 
> full 
> >> > description. 
> >> > --- 
> >> > 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+...@googlegroups.com. 
> >> > To post to this group, send email to sqlal...@googlegroups.com. 
> >> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> >> > For more options, visit https://groups.google.com/d/optout. 
> > 
> > -- 
> > SQLAlchemy - 
> > The Python SQL Toolkit and Object Relational Mapper 
> > 
> > http://www.sqlalchemy.org/ 
> > 
> > To post example code, please provide an MCVE: Minimal, Complete, and 
> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> > description. 
> > --- 
> > 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+...@googlegroups.com <javascript:>. 
> > To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>. 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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