'AutogenContext' object has no attribute '__getitem__'

2015-10-13 Thread Fayaz Yusuf Khan
Hi,
Looks like the autogen_context parameter has changed type recently.

I'm getting this exception:

Traceback (most recent call last):
  File "/home/fayaz/Programming/weaver-env/bin/weaver", line 9, in 
load_entry_point('weaver==2.12.1', 'console_scripts', 'weaver')()
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py",
 
line 700, in __call__
return self.main(*args, **kwargs)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py",
 
line 680, in main
rv = self.invoke(ctx)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py",
 
line 1027, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py",
 
line 1027, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py",
 
line 873, in invoke
return ctx.invoke(self.callback, **ctx.params)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py",
 
line 508, in invoke
return callback(*args, **kwargs)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/decorators.py",
 
line 16, in new_func
return f(get_current_context(), *args, **kwargs)
  File "/home/fayaz/Programming/weaver-backend/weaver/cli.py", line 44, in 
script
weaver.db.generate_migrate_script(url=ctx.obj, message=message)
  File "/home/fayaz/Programming/weaver-backend/weaver/db/sql/__init__.py", 
line 132, in generate_migrate_script
make_config(url), message, autogenerate=True)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/command.py",
 
line 121, in revision
revision_context.generate_scripts()
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/api.py",
 
line 412, in generate_scripts
yield self._to_script(generated_revision)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/api.py",
 
line 336, in _to_script
autogen_context, migration_script, template_args
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 40, in _render_python_into_templatevars
_render_cmd_body(upgrade_ops, autogen_context))
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 63, in _render_cmd_body
lines = render_op(autogen_context, op)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 75, in render_op
lines = util.to_list(renderer(autogen_context, op))
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 102, in _render_modify_table
t_lines = render_op(autogen_context, t_op)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 75, in render_op
lines = util.to_list(renderer(autogen_context, op))
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 299, in _add_column
"column": _render_column(column, autogen_context),
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 542, in _render_column
'type': _repr_type(column.type, autogen_context),
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 565, in _repr_type
rendered = _user_defined_render("type", type_, autogen_context)
  File 
"/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py",
 
line 513, in _user_defined_render
rendered = render(type_, object_, autogen_context)
  File 
"/home/fayaz/Programming/weaver-backend/weaver/db/migrations/env.py", line 
52, in render_item
autogen_context['imports'].add(



Here's the code:

def render_item(type_, obj, autogen_context):
from weaver.core.model import PriceType
if type_ == 'type':
if isinstance(obj, PriceType):
print autogen_context
autogen_context['imports'].add(
'from weaver.core.model import PriceType')
return '%r' % obj
return False


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


[sqlalchemy] Proper syntax for __table_args__ ?

2015-10-13 Thread Don O'Hara
I'm looking for advice on best SA coding practices to accomplish:

1. Use bind keys
2. Add user-defined tags to tables, to allow iterating through the metadata 
later to find tables with those tags.
3. Use base table classes for common attributes and methods

Is there a chance that class member names will get mangled in derived 
classes,
and cause problems somewhere downstream? The code works now, but when I 
remove one of the
base classes, and code the metadata directly in the table class things 
break.

I'd like to get  a better understanding of how it all fits, and make sure 
I'm setting things up correctly.

Thanks,
Don



My code:

*in config.*
*py*
SQLALCHEMY_BINDS = {
'api': 'postgresql+psycopg2://oharad:dino@localhost:5432/dev_api_02',
'person_indexes': 
'postgresql+psycopg2://oharad:dino@localhost:5432/dev_person_ix_01',
'analysis_result': 
'postgresql+psycopg2://oharad:dino@localhost:5432/dev_analysis_res_01'
}

*in models/__init__.py*

ACCOUNT_SCHEMA_NAMESPACE = 'account'

class BaseAppTable(db.Model):
__abstract__ = True
def to_json(self):
json = {c.name: str(getattr(self, c.name)) or '' for c in 
self.__table__.columns}
return json


*in models/account.py*


from models import BaseAppTable, db, ACCOUNT_SCHEMA_NAMESPACE

_BASE_TABLE_ARGS = {'info': {'table_namespace': ACCOUNT_SCHEMA_NAMESPACE}}

class BaseAccountTable(object):
  __bind_key__ = 'api'
  __table_args__ = _BASE_TABLE_ARGS

class Account(BaseAppTable, BaseAccountTable):
__tablename__ = "account"
#
# need to explicitly code __table_args__ here to use 'tuple, dict' 
syntax
__table_args__ = (UniqueConstraint('name', 'company_id', 
name='account_uq_ix1'), 
  _BASE_TABLE_ARGS)
account_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(length=50), nullable=False)
company_id = db.Column(db.Integer, 
db.ForeignKey('global.company.company_id'), nullable=False)


class RoleGroup(BaseAppTable, BaseAccountTable):
__tablename__ = "role_group"
role_group_id = db.Column(UUID, primary_key=True)
type_code = db.Column(db.SmallInteger, nullable=False)
name = db.Column(db.String(length=100), nullable=False, unique=True)


This code works fine, but I'm worried something might break due to name 
mangling.

When I change the code to avoid base classes, I get an error:

class AccountCodeNamespace(BaseAppTable):
  __tablename__ = "account_code_namespace"
  __table_args__ = _BASE_TABLE_ARGS
  __bind_key__ = 'api'



*  #  AssertionError: Bind 'api' is not specified.  Set it in the 
SQLALCHEMY_BINDS configuration variable*



-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Proper syntax for __table_args__ ?

2015-10-13 Thread Mike Bayer


On 10/13/15 7:16 AM, Don O'Hara wrote:
> I'm looking for advice on best SA coding practices to accomplish:
> 
> 1. Use bind keys

I'm not familiar with that, I see you using something called
__bind_key__ and SQLALCHEMY_BINDS.  These are not constructs that are
part of SQLAlchemy; if this is a third-party package of some kind, I'd
ask them for help.





> 2. Add user-defined tags to tables, to allow iterating through the
> metadata later to find tables with those tags.
> 3. Use base table classes for common attributes and methods
> 
> Is there a chance that class member names will get mangled in derived
> classes,
> and cause problems somewhere downstream? The code works now, but when I
> remove one of the
> base classes, and code the metadata directly in the table class things
> break.
> 
> I'd like to get  a better understanding of how it all fits, and make
> sure I'm setting things up correctly.
> 
> Thanks,
> Don
> 
> 
> 
> My code:
> 
> |
> _inconfig.__py
> _
> SQLALCHEMY_BINDS ={
> 'api':'postgresql+psycopg2://oharad:dino@localhost:5432/dev_api_02',
>
> 'person_indexes':'postgresql+psycopg2://oharad:dino@localhost:5432/dev_person_ix_01',
>
> 'analysis_result':'postgresql+psycopg2://oharad:dino@localhost:5432/dev_analysis_res_01'
> }
> 
> _in models/__init__.py_
> 
> ACCOUNT_SCHEMA_NAMESPACE = 'account'
> 
> class BaseAppTable(db.Model):
> __abstract__ = True
> def to_json(self):
> json = {c.name: str(getattr(self, c.name)) or '' for c in
> self.__table__.columns}
> return json
> 
> 
> _in models/account.py_
> 
> 
> from models import BaseAppTable, db, ACCOUNT_SCHEMA_NAMESPACE
> 
> _BASE_TABLE_ARGS = {'info': {'table_namespace': ACCOUNT_SCHEMA_NAMESPACE}}
> 
> class BaseAccountTable(object):
>   __bind_key__ = 'api'
>   __table_args__ = _BASE_TABLE_ARGS
> 
> class Account(BaseAppTable, BaseAccountTable):
> __tablename__ = "account"
> #
> # need to explicitly code __table_args__ here to use 'tuple, dict'
> syntax
> __table_args__ = (UniqueConstraint('name', 'company_id',
> name='account_uq_ix1'), 
>   _BASE_TABLE_ARGS)
> account_id = db.Column(db.Integer, primary_key=True)
> name = db.Column(db.String(length=50), nullable=False)
> company_id = db.Column(db.Integer,
> db.ForeignKey('global.company.company_id'), nullable=False)
> 
> 
> class RoleGroup(BaseAppTable, BaseAccountTable):
> __tablename__ = "role_group"
> role_group_id = db.Column(UUID, primary_key=True)
> type_code = db.Column(db.SmallInteger, nullable=False)
> name = db.Column(db.String(length=100), nullable=False, unique=True)
> 
> |
> 
> This code works fine, but I'm worried something might break due to name
> mangling.
> 
> When I change the code to avoid base classes, I get an error:
> 
> |
> classAccountCodeNamespace(BaseAppTable):
>   __tablename__ ="account_code_namespace"
>   __table_args__ =_BASE_TABLE_ARGS
>   __bind_key__ ='api'
> 
> |
> 
> 
> *  #  AssertionError: Bind 'api' is not specified.  Set it in the
> SQLALCHEMY_BINDS configuration variable*
> 
> 
> 
> -- 
> 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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Problem with hybrid_property

2015-10-13 Thread Cecilio Ruiz
Everything works fine, add, modify, etc. But when I ask Caja.Imp. He says 
he does not exist.


# Table definition - Caja
# 
Caja_table = sa.Table("Caja", metadata,
sa.Column('id', sa.Integer, nullable=True, autoincrement=True, 
primary_key=True),
sa.Column('Entidad', sa.String, nullable=True),
sa.Column('Fecha', sa.Date, nullable=True),
sa.Column('Entrada', sa.Float, default=0, nullable=True),
sa.Column('Salida', sa.Float, default=0, nullable=True))



class Caja(object):
def __init__(self, Entidad, Fecha, Entrada, Salida):
self.Entidad = Entidad
self.Fecha = Fecha
self.Entrada = Entrada
self.Salida = Salida

@hybrid_property
*def imp(self):*
return float(self.Entrada - self.Salida)

def __repr__(self):
return "" % (self.id, self.id_Conceptos, self.Entidad, self.Fecha, 
self.Grupo, self.Entrada, self.Salida, self.imp)

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Proper syntax for __table_args__ ?

2015-10-13 Thread Don O'Hara
Oops - should have asked the Flask-SQLAlchemy folks!

Thanks

On Tue, Oct 13, 2015 at 11:30 AM, Mike Bayer 
wrote:

>
>
> On 10/13/15 7:16 AM, Don O'Hara wrote:
> > I'm looking for advice on best SA coding practices to accomplish:
> >
> > 1. Use bind keys
>
> I'm not familiar with that, I see you using something called
> __bind_key__ and SQLALCHEMY_BINDS.  These are not constructs that are
> part of SQLAlchemy; if this is a third-party package of some kind, I'd
> ask them for help.
>
>
>
>
>
> > 2. Add user-defined tags to tables, to allow iterating through the
> > metadata later to find tables with those tags.
> > 3. Use base table classes for common attributes and methods
> >
> > Is there a chance that class member names will get mangled in derived
> > classes,
> > and cause problems somewhere downstream? The code works now, but when I
> > remove one of the
> > base classes, and code the metadata directly in the table class things
> > break.
> >
> > I'd like to get  a better understanding of how it all fits, and make
> > sure I'm setting things up correctly.
> >
> > Thanks,
> > Don
> >
> >
> >
> > My code:
> >
> > |
> > _inconfig.__py
> > _
> > SQLALCHEMY_BINDS ={
> > 'api':'postgresql+psycopg2://oharad:dino@localhost:5432/dev_api_02',
> >
> > 'person_indexes':'postgresql+psycopg2://oharad:dino@localhost
> :5432/dev_person_ix_01',
> >
> > 'analysis_result':'postgresql+psycopg2://oharad:dino@localhost
> :5432/dev_analysis_res_01'
> > }
> >
> > _in models/__init__.py_
> >
> > ACCOUNT_SCHEMA_NAMESPACE = 'account'
> >
> > class BaseAppTable(db.Model):
> > __abstract__ = True
> > def to_json(self):
> > json = {c.name: str(getattr(self, c.name)) or '' for c in
> > self.__table__.columns}
> > return json
> >
> >
> > _in models/account.py_
> >
> >
> > from models import BaseAppTable, db, ACCOUNT_SCHEMA_NAMESPACE
> >
> > _BASE_TABLE_ARGS = {'info': {'table_namespace':
> ACCOUNT_SCHEMA_NAMESPACE}}
> >
> > class BaseAccountTable(object):
> >   __bind_key__ = 'api'
> >   __table_args__ = _BASE_TABLE_ARGS
> >
> > class Account(BaseAppTable, BaseAccountTable):
> > __tablename__ = "account"
> > #
> > # need to explicitly code __table_args__ here to use 'tuple, dict'
> > syntax
> > __table_args__ = (UniqueConstraint('name', 'company_id',
> > name='account_uq_ix1'),
> >   _BASE_TABLE_ARGS)
> > account_id = db.Column(db.Integer, primary_key=True)
> > name = db.Column(db.String(length=50), nullable=False)
> > company_id = db.Column(db.Integer,
> > db.ForeignKey('global.company.company_id'), nullable=False)
> >
> >
> > class RoleGroup(BaseAppTable, BaseAccountTable):
> > __tablename__ = "role_group"
> > role_group_id = db.Column(UUID, primary_key=True)
> > type_code = db.Column(db.SmallInteger, nullable=False)
> > name = db.Column(db.String(length=100), nullable=False, unique=True)
> >
> > |
> >
> > This code works fine, but I'm worried something might break due to name
> > mangling.
> >
> > When I change the code to avoid base classes, I get an error:
> >
> > |
> > classAccountCodeNamespace(BaseAppTable):
> >   __tablename__ ="account_code_namespace"
> >   __table_args__ =_BASE_TABLE_ARGS
> >   __bind_key__ ='api'
> >
> > |
> >
> >
> > *  #  AssertionError: Bind 'api' is not specified.  Set it in the
> > SQLALCHEMY_BINDS configuration variable*
> >
> >
> >
> > --
> > 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 http://groups.google.com/group/sqlalchemy.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> 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/7kb-9pSu3Bk/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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] emulating psql's "\copy" function?

2015-10-13 Thread Jon Nelson
On Tue, Oct 13, 2015 at 2:49 PM, Jon Nelson  wrote:
> On Tue, Oct 13, 2015 at 1:55 PM, Jonathan Vanasco  
> wrote:
>> As part of an archiving routine that uses SqlAlchemy, I need to execute some
>> pretty specific commands using `\copy` to archive a selection of columns, in
>> a special order, into a csv.
>>
>> Doing some digging, psycopg2 provides an interface to `COPY` -- but that
>> doesn't work for my needs.
>> I'd rather not use a subprocess to handle run `\copy` in psql, because then
>> it's not in the transaction.
>>
>> I'll be running this to partition 10GB of data into a lot of 10-50MB
>> chunks... so I'd like to avoid piping this through sqlalchemy.  i'm not
>> opposed to pulling this in row-by-row, but I'd really rather avoid it.  I
>> have almost 250MM rows right now, and the future "nightly" build will be
>> doing about 1MM at a time.
>>
>> Does anyone have a suggestion for a workaround?
>
> I should be able to provide you with something, as I use this
> functionality quite often. However, I must step away from my desk for
> the moment and thus there will be a delay.

This is what I've done, and it might not be the best way to do things
but it works for me:

one way or another, get a connection (either from your session or your
active connection, etc..):

c = get_your_connection()
with c.connection.cursor() as cursor:
cursor.copy_expert(sql_statement_here, some_writable_file)
# or
cursor.copy_from(some_readable_file, tablename, )
# or
cursor.copy_to(some_writable_file, tablename, )


I didn't place all of the arguments in the calls above, refer to the
psycopg2 docs for that. The above is almost pseudo-code. I also take
pains to properly quote the table names, etc. whenever that
information is generated or passed (and often even when hardcoded).

Hope this helps.



-- 
Jon

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] emulating psql's "\copy" function?

2015-10-13 Thread Jon Nelson
On Tue, Oct 13, 2015 at 1:55 PM, Jonathan Vanasco  wrote:
> As part of an archiving routine that uses SqlAlchemy, I need to execute some
> pretty specific commands using `\copy` to archive a selection of columns, in
> a special order, into a csv.
>
> Doing some digging, psycopg2 provides an interface to `COPY` -- but that
> doesn't work for my needs.
> I'd rather not use a subprocess to handle run `\copy` in psql, because then
> it's not in the transaction.
>
> I'll be running this to partition 10GB of data into a lot of 10-50MB
> chunks... so I'd like to avoid piping this through sqlalchemy.  i'm not
> opposed to pulling this in row-by-row, but I'd really rather avoid it.  I
> have almost 250MM rows right now, and the future "nightly" build will be
> doing about 1MM at a time.
>
> Does anyone have a suggestion for a workaround?

I should be able to provide you with something, as I use this
functionality quite often. However, I must step away from my desk for
the moment and thus there will be a delay.


-- 
Jon

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] emulating psql's "\copy" function?

2015-10-13 Thread Jonathan Vanasco
As part of an archiving routine that uses SqlAlchemy, I need to execute 
some pretty specific commands using `\copy` to archive a selection of 
columns, in a special order, into a csv.

Doing some digging, psycopg2 provides an interface to `COPY` -- but that 
doesn't work for my needs.  
I'd rather not use a subprocess to handle run `\copy` in psql, because then 
it's not in the transaction.

I'll be running this to partition 10GB of data into a lot of 10-50MB 
chunks... so I'd like to avoid piping this through sqlalchemy.  i'm not 
opposed to pulling this in row-by-row, but I'd really rather avoid it.  I 
have almost 250MM rows right now, and the future "nightly" build will be 
doing about 1MM at a time.

Does anyone have a suggestion for a workaround?

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Problem with hybrid_property

2015-10-13 Thread Cecilio Ruiz
The error message says: : "AttributeError: 'Caja' object has no attribute 
'imp'

Yes, is typo error en email. The imp attibute is lower case.

El martes, 13 de octubre de 2015, 23:01:34 (UTC+2), Simon King escribió:
>
>   return " Imp:%.2f)>" % (self.id, self.id_Conceptos, self.Entidad, self.Fecha, 
> self.Grupo, self.Entrada, self.Salida, self.imp) 
> > 
>
> Can you share the full traceback? Caja.Imp doesn’t exist, but Caja.imp 
> (note lower case “i”) should, so assuming that was a typo in your email, 
> there must be some other problem. 
>
> Simon

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Problem with hybrid_property

2015-10-13 Thread Simon King
Do you perhaps have more than one “Caja” class in your application? Try 
printing out some of the following values:

your_instance.__dict__
type(your_instance)
type(your_instance).__dict__
sys.modules[type(your_instance).__module__]

Can you drop into pdb at the point where the exception occurs and poke around 
at the object interactively?

Otherwise, you’re going to have to send us a complete script that demonstrates 
the problem.

Simon

> On 13 Oct 2015, at 22:37, Cecilio Ruiz  wrote:
> 
> The error message says: : "AttributeError: 'Caja' object has no attribute 
> 'imp'
> 
> Yes, is typo error en email. The imp attibute is lower case.
> 
> El martes, 13 de octubre de 2015, 23:01:34 (UTC+2), Simon King escribió:
>   return " Imp:%.2f)>" % (self.id, self.id_Conceptos, self.Entidad, self.Fecha, 
> self.Grupo, self.Entrada, self.Salida, self.imp) 
> > 
> 
> Can you share the full traceback? Caja.Imp doesn’t exist, but Caja.imp (note 
> lower case “i”) should, so assuming that was a typo in your email, there must 
> be some other problem. 
> 
> Simon
> 
> -- 
> 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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Problem with hybrid_property

2015-10-13 Thread Simon King
> On 13 Oct 2015, at 17:09, Cecilio Ruiz  wrote:
> 
> Everything works fine, add, modify, etc. But when I ask Caja.Imp. He says he 
> does not exist.
> 
> 
> # Table definition - Caja
> # 
> Caja_table = sa.Table("Caja", metadata,
> sa.Column('id', sa.Integer, nullable=True, autoincrement=True, 
> primary_key=True),
> sa.Column('Entidad', sa.String, nullable=True),
> sa.Column('Fecha', sa.Date, nullable=True),
> sa.Column('Entrada', sa.Float, default=0, nullable=True),
> sa.Column('Salida', sa.Float, default=0, nullable=True))
> 
> 
> 
> class Caja(object):
> def __init__(self, Entidad, Fecha, Entrada, Salida):
> self.Entidad = Entidad
> self.Fecha = Fecha
> self.Entrada = Entrada
> self.Salida = Salida
> 
> @hybrid_property
> def imp(self):
> return float(self.Entrada - self.Salida)
> 
> def __repr__(self):
> return " Imp:%.2f)>" % (self.id, self.id_Conceptos, self.Entidad, self.Fecha, 
> self.Grupo, self.Entrada, self.Salida, self.imp)
> 

Can you share the full traceback? Caja.Imp doesn’t exist, but Caja.imp (note 
lower case “i”) should, so assuming that was a typo in your email, there must 
be some other problem.

Simon

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: history_meta.py: foreign keys in history table, and relationships

2015-10-13 Thread Alex Fraser
On Tuesday, 25 August 2015 11:23:51 UTC+10, Alex Fraser wrote:
>
> Is there a declarative way to add foreign keys to the history table when 
> using history_meta.py? In the app I'm making the user can view old versions 
> of a document, and I want to make sure e.g. the user that created the old 
> version can't be deleted while their old versions exist.
>

For what it's worth, we have solved this by checking for a 'version' flag 
in the info attribute of foreign key constraints:

diff --git a/history_meta.py b/history_meta.py
index f1e308e..da72f27 100644
--- a/history_meta.py
+++ b/history_meta.py
@@ -117,6 +117,16 @@ def _history_mapper(local_mapper):
 *cols,
 schema=local_mapper.local_table.schema
 )
+
+for fk in local_mapper.local_table.foreign_key_constraints:
+log.debug(
+"Duplicating foreign key for history table: %s, fk: %s",
+local_mapper.local_table, fk)
+if 'version' in fk.info and fk.info['version']:
+fk_new = fk.copy()
+fk_new.info['history_copy'] = fk
+table.append_constraint(fk_new)
+
 else:
 # single table inheritance.  take any additional columns that may 
have
 # been added and add them to the history table.

Let me know if this is too dodgy :)

Cheers,
Alex

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.