Re: [sqlalchemy] where is InstanceState.key set?

2016-04-21 Thread Mike Bayer



On 04/21/2016 07:22 AM, Chris Withers wrote:

I ended up going with
https://github.com/cjw296/chide/blob/master/chide/sqlalchemy.py#L26
having read those.
Sounds about right?


oh.  I thought you wanted the code that *assigned* state.key.  The code 
to just figure out what the key should be is public:


http://docs.sqlalchemy.org/en/rel_1_0/orm/mapping_api.html#sqlalchemy.orm.util.identity_key






On 21/04/2016 00:23, Mike Bayer wrote:


on the load side in loading.py and on the persistence side in
session.py _register_newly_persistent.



On 04/20/2016 01:28 PM, Chris Withers wrote:

Hey All,

Where is InstanceState.key set?

I'm looking for the code that builds the key used in the identity_map of
the session. Turns out to be not so easy to find...






Chris







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


[sqlalchemy] chide 2.0.0 released! - sample objects for tests

2016-04-21 Thread Chris Withers

  
  
Hi All,

I've just released a new version of this tiny
library for making sample objects for testing.

Here's a SQLAlchemy example:

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship('Child')

class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
value = Column(Integer)
Now we can build a source of sample data:

from chide import Collection
from chide.sqlalchemy import Set

data = Collection({
Parent: {'id': 1, 'child': Child},
Child: {'id': 3, 'value': 42}
})

And finally, we can use it:

>>> session = Sess
 i
on()
>>> samples = Set(data)
>>> session.add(samples.get(Parent))
>>> session.commit()
>>> session.query(Parent).one()

>>> _.child

The big change was realising that Collections tend to be module-level, so having them own the set of unique instances is bad.
So, that's now split out into a Set object, which should be instantiated one per test!

Documentation is here: http://chide.readthedocs.org/en/latest/
Development happens here: https://github.com/cjw296/chide/

cheers,



Chris





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


Re: [sqlalchemy] where is InstanceState.key set?

2016-04-21 Thread Chris Withers
I ended up going with 
https://github.com/cjw296/chide/blob/master/chide/sqlalchemy.py#L26 
having read those.

Sounds about right?

On 21/04/2016 00:23, Mike Bayer wrote:


on the load side in loading.py and on the persistence side in 
session.py _register_newly_persistent.




On 04/20/2016 01:28 PM, Chris Withers wrote:

Hey All,

Where is InstanceState.key set?

I'm looking for the code that builds the key used in the identity_map of
the session. Turns out to be not so easy to find...






Chris





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


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: How to handle changing formerly-unnamed constraints (and retrying after errors in migration scripts)

2016-04-21 Thread Ben Sizer
Aha, thanks for that. For the benefit of anyone who comes across this 
later, my final script looked much like this:

def downgrade():
try:
inner_transaction = op.get_bind().begin_nested() # establish a 
savepoint
with op.batch_alter_table("user_authentication_method") as batch_op:
batch_op.drop_constraint(composite_key_name, type_='unique')
except ProgrammingError:
# Try again, with the name we probably have for it, at least in 
Postgres
inner_transaction.rollback()
probable_name = "user_authentication_method_pkey"
with op.batch_alter_table("user_authentication_method") as batch_op:
batch_op.drop_constraint(probable_name, type_='unique')

No guarantees that the above is /correct/ but it catches the exception and 
seems to work so far.

Thanks again.

-- 
Ben


On Wednesday, 20 April 2016 14:52:14 UTC+1, Mike Bayer wrote:


On 04/20/2016 07:42 AM, Ben Sizer wrote: 
> I had an unnamed UniqueConstraint, and autogenerated upgrade scripts 
> that passed in None as the name - these worked perfectly. Now I am 
> discovering that the downgrade doesn't work, as there is no name to 
> refer to the constraint (i.e. "sqlalchemy.exc.CompileError: Can't emit 
> DROP CONSTRAINT for constraint UniqueConstraint(); it has no name") 
> 
> So, I'm trying to add a name in the model, and edit the migration 
> scripts accordingly. My strategy was this: 
> 
> - the upgrade function would add the constraint with a name in future 
> - the downgrade function needs to be able to delete the constraint 
> whatever it was called 
> 
> The first is easy enough and seems to have no problems. 
> 
> The second is tricky; if the name passed to drop_constraint is wrong, it 
> raises the expected error (e.g. sqlalchemy.exc.ProgrammingError: 
> (psycopg2.ProgrammingError) constraint "provider_name_user_id_key" of 
> relation "user_authentication_method" does not exist) 
> What I'd like to do, is catch that exception, and try another name (e.g. 
> the name that I know was autogenerated, in this case 
> 'user_authentication_method_pkey') - however, attempting to catch the 
> error and perform a further operation emits a Postgres internal error 
> that suggests I need to end the current transaction and start a new one 
> (i.e. sqlalchemy.exc.InternalError: (psycopg2.InternalError) current 
> transaction is aborted, commands ignored until end of transaction block) 
> and I can't find out how to do that. I tried calling rollback() on a 
> session created around the return value of get_bind() but the error was 
> the same. 

You should be able to use a savepoint around that operation to prevent 
this issue from happening. This works for simple things like INSERTs 
and hopefully works for DDL as well. Either with an ORM Session or with 
the Connection, the begin_nested() method gives you a savepoint 
transaction. 

> 
> To complicate matters, one of the possible DBs is SQLite so I have to do 
> this via op.batch_etc. Thankfully that data is not essential so deleting 
> the whole DB is an option there - not so much for the Postgres data. 

For SQLite I've always been a proponent of dropping the whole thing and 
recreating from scratch. The existence of batch mode is due to all the 
pressure I get from users who don't want to do it that way, but I still 
favor not trying to make SQLite do real migrations - its creators 
disagree with this use case hence they've never implemented it. 

> 
> So my questions are: 
> 
> 1) How can I handle this drop_constraint call when we didn't specify a 
> constraint name in the first place? 
> 2) If the best way is for me to just attempt to drop constraints by 
> name, trying several names, how can I catch or avoid the first error and 
> try subsequent constraint names? 
> 
> Thanks, 
> Ben 

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