I'm trying to achieve what it states in the documents that I linked to. "if True, the synonym() <http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html#sqlalchemy.orm.synonym> construct will locate the existing named MapperProperty <http://docs.sqlalchemy.org/en/latest/orm/internals.html#sqlalchemy.orm.interfaces.MapperProperty> based on the attribute name of this synonym() <http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html#sqlalchemy.orm.synonym>, and assign it to a new attribute linked to the name of this synonym() <http://docs.sqlalchemy.org/en/latest/orm/mapped_attributes.html#sqlalchemy.orm.synonym>. That is, given a mapping like: " .... example code ... "The above class MyClass will now have the job_status Column <http://docs.sqlalchemy.org/en/latest/core/metadata.html#sqlalchemy.schema.Column> object mapped to the attribute named _job_status, and the attribute named job_status will refer to the synonym itself. This feature is typically used in conjunction with the descriptor argument in order to link a user-defined descriptor as a “wrapper” for an existing column."
On Wednesday, January 24, 2018 at 9:00:21 PM UTC-8, Mike Bayer wrote: > > On Wed, Jan 24, 2018 at 10:17 PM, dykang <dyk...@gmail.com <javascript:>> > wrote: > > 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? > > It has never worked that way, and I don't understand what that would > do. There's a column, "job_status", and your class has a "job_status" > attribute mapped to it, and you're done. What does the synonym > accomplish ? In my case I'm trying to attach an accessor to the job_status that changes the gets/sets. The synonym allows me to create a getter/setter without the need to expose that both exist. The section of the docs that I linked to seem to explicitly state that it should work that way. Maybe I'm misunderstanding what the docs mean, but the text seems to be clearly indicating that what I'm asking should be possible, and even indicates what a typical use would be. > What does "job_status column is never created" mean, you > don't actually want that column in the database? > > I mean that if I try to follow the example in the documentation and allow both the column and the synonym to have the same name, the table object will not have a job_status column in it's mapper. see this example/output ---------------------------- from sqlalchemy.ext import declarative from sqlalchemy import * from sqlalchemy.orm import * from db import dbbase Base = declarative.declarative_base() class MyClass(Base): __tablename__ = 'my_table' id = Column(Integer, primary_key=True) job_status = Column(String(50), default = '') job_status = synonym("_job_status") e = create_engine("sqlite://", echo=True) Base.metadata.create_all(e) 2018-01-25 02:58:50,028 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1 2018-01-25 02:58:50,028 INFO sqlalchemy.engine.base.Engine () 2018-01-25 02:58:50,029 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1 2018-01-25 02:58:50,029 INFO sqlalchemy.engine.base.Engine () 2018-01-25 02:58:50,030 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("my_table") 2018-01-25 02:58:50,030 INFO sqlalchemy.engine.base.Engine () 2018-01-25 02:58:50,031 INFO sqlalchemy.engine.base.Engine CREATE TABLE my_table ( id INTEGER NOT NULL, PRIMARY KEY (id) ) ---------------------- No job_status column was created when the table was created. > > > > > > 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. > > not clear what you're looking to achieve. > I'm not actually sure anymore. Initially I was looking for a way to have the class attribute be the same name as the column in the db, while also providing custom accessors to the data field. So I was hoping to declare the attr with the right name, then apply a synonym to it with the same name and my custom accessor. But then after all this thread, I realized I could just name the column manually and not try to let the declarative api name the actual table column for me. I still think there's something I'm not understanding regarding what I linked to in the docs, but I got something working, it seems. Thanks for the pointers. > > > > > > > > > > > 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> 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. > >> > 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.