Re: [sqlalchemy] Accessing name of the table in class' attributes.

2016-04-21 Thread Mike Bayer



On 04/21/2016 06:00 AM, Piotr Dobrogost wrote:

On Wednesday, April 20, 2016 at 3:38:01 PM UTC+2, Mike Bayer wrote:


you can put "id" in declared_attr, should work:

class Model(Base):
  @declared_attr
  def id(cls):
  return Column(Integer, Sequence(cls.__tablename__ +
"id_seq"), ...)



Id does not work due to "sqlalchemy.exc.ArgumentError: Column-based
expression object expected for argument 'remote_side'; got:
'', type " error. It looks like
remote_side is not aware of declared_attr. Should it be?


Traceback (most recent call last):
   File
"/opt/pycharm/pycharm-2016.1.2/helpers/pycharm/pycharm_load_entry_point.py",
line 12, in 
 sys.exit(f())
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/pyramid/scripts/pserve.py",
line 60, in main
 return command.run()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/pyramid/scripts/pserve.py",
line 367, in run
 global_conf=vars)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/pyramid/scripts/pserve.py",
line 402, in loadapp
 return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
line 247, in loadapp
 return loadobj(APP, uri, name=name, **kw)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
line 272, in loadobj
 return context.create()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
line 710, in create
 return self.object_type.invoke(self)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
line 203, in invoke
 app = context.app_context.create()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
line 710, in create
 return self.object_type.invoke(self)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
line 146, in invoke
 return fix_call(context.object, context.global_conf,
**context.local_conf)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/util.py",
line 55, in fix_call
 val = callable(*args, **kw)
   File "/home/piotr/projects/kotti/kotti/__init__.py", line 188, in main
 initialize_sql(engine)
   File "/home/piotr/projects/kotti/kotti/resources.py", line 885, in
initialize_sql
 populate()
   File "/home/piotr/projects/kotti/kotti/populate.py", line 47, in populate
 if DBSession.query(Node.id).count() == 0:
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py",
line 157, in do
 return getattr(self.registry(), name)(*args, **kwargs)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 1260, in query
 return self._query_cls(entities, self, **kwargs)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 110, in __init__
 self._set_entities(entities)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 120, in _set_entities
 self._set_entity_selectables(self._entities)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 137, in _set_entity_selectables
 ext_info.mapper._equivalent_columns
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 153, in _mapper_loads_polymorphically_with
 for m2 in mapper._with_polymorphic_mappers or [mapper]:
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
line 754, in __get__
 obj.__dict__[self.__name__] = result = self.fget(obj)
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
line 1893, in _with_polymorphic_mappers
 configure_mappers()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
line 2768, in configure_mappers
 mapper._post_configure_properties()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
line 1710, in _post_configure_properties
 prop.init()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py",
line 183, in init
 self.do_init()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
line 1628, in do_init
 self._process_dependent_arguments()
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
line 1683, in _process_dependent_arguments
 util.to_column_set(self.remote_side))
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
line 1682, in 
 for x in
   File
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packag

Re: [sqlalchemy] Accessing name of the table in class' attributes.

2016-04-21 Thread Piotr Dobrogost
On Wednesday, April 20, 2016 at 3:38:01 PM UTC+2, Mike Bayer wrote:
>
>
> you can put "id" in declared_attr, should work: 
>
> class Model(Base): 
>  @declared_attr 
>  def id(cls): 
>  return Column(Integer, Sequence(cls.__tablename__ + "id_seq"), 
> ...) 
>


Id does not work due to "sqlalchemy.exc.ArgumentError: Column-based 
expression object expected for argument 'remote_side'; got: 
'', 
type " error. It 
looks like remote_side is not aware of declared_attr. Should it be?


Traceback (most recent call last):
  File 
"/opt/pycharm/pycharm-2016.1.2/helpers/pycharm/pycharm_load_entry_point.py", 
line 12, in 
sys.exit(f())
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/pyramid/scripts/pserve.py",
 
line 60, in main
return command.run()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/pyramid/scripts/pserve.py",
 
line 367, in run
global_conf=vars)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/pyramid/scripts/pserve.py",
 
line 402, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
 
line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
 
line 272, in loadobj
return context.create()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
 
line 710, in create
return self.object_type.invoke(self)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
 
line 203, in invoke
app = context.app_context.create()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
 
line 710, in create
return self.object_type.invoke(self)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/loadwsgi.py",
 
line 146, in invoke
return fix_call(context.object, context.global_conf, 
**context.local_conf)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/paste/deploy/util.py",
 
line 55, in fix_call
val = callable(*args, **kw)
  File "/home/piotr/projects/kotti/kotti/__init__.py", line 188, in main
initialize_sql(engine)
  File "/home/piotr/projects/kotti/kotti/resources.py", line 885, in 
initialize_sql
populate()
  File "/home/piotr/projects/kotti/kotti/populate.py", line 47, in populate
if DBSession.query(Node.id).count() == 0:
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py",
 
line 157, in do
return getattr(self.registry(), name)(*args, **kwargs)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 1260, in query
return self._query_cls(entities, self, **kwargs)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
 
line 110, in __init__
self._set_entities(entities)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
 
line 120, in _set_entities
self._set_entity_selectables(self._entities)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
 
line 137, in _set_entity_selectables
ext_info.mapper._equivalent_columns
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
 
line 153, in _mapper_loads_polymorphically_with
for m2 in mapper._with_polymorphic_mappers or [mapper]:
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
 
line 754, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 1893, in _with_polymorphic_mappers
configure_mappers()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 2768, in configure_mappers
mapper._post_configure_properties()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 1710, in _post_configure_properties
prop.init()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py",
 
line 183, in init
self.do_init()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
 
line 1628, in do_init
self._process_dependent_arguments()
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
 
line 1683, in _process_dependent_arguments
util.to_column_set(self.remote_side))
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
 
line 1682, in 
for x in
  File 
"/home/piotr/.virtualenvs/kotti/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",

Re: [sqlalchemy] Accessing name of the table in class' attributes.

2016-04-20 Thread Mike Bayer



On 04/20/2016 07:35 AM, Piotr Dobrogost wrote:

Having this code

class Base(object):
 @declared_attr
 def __tablename__(cls):
 return '{0}s'.format(camel_case_to_name(cls.__name__))


class Model(Base):
 id = Column(Integer, Sequence(???, optional=True), primary_key=True)


is there a way to use name of Model's table inside declaration of "id"
column?


you can put "id" in declared_attr, should work:

class Model(Base):
@declared_attr
def id(cls):
return Column(Integer, Sequence(cls.__tablename__ + "id_seq"), ...)


The other way would be to name the Sequence after the fact using the 
after_parent_attach event.   This is how the naming convention system 
works which currently does not include Sequences but perhaps it should.







Regards,
Piotr Dobrogost

--
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.