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

2016-04-20 Thread Mike Bayer


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] where is InstanceState.key set?

2016-04-20 Thread Chris Withers

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] Re: __table_alias__ option

2016-04-20 Thread SPetro
Thank you

On Wednesday, April 20, 2016 at 10:25:43 AM UTC-4, SPetro wrote:
>
> Hi, is possible to have "default" alias for table
>
> class Cwle(Base):
> __tablename__ = 'CardiacWaitListEntry'
> __table_alias__ = 'CWLE'
>
> I know you can do this
> Cwle = aliased(Cwle, name='CWLE')
>
> but this screw up any editor intellisense. Output of sqalchemy query would 
> be more readable and what we use as convention.
>  
>
>
>
>
>
>
>
>

-- 
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] Re: __table_alias__ option

2016-04-20 Thread Mike Bayer


if intellisense is scanning source code to determine what elements an 
object has, then I don't see any way for it to do this unless you 
actually typed in a new definition of some kind that laid out all the 
attributes you'd like intellisense to know about.   IMO such tools are 
not practical for dynamic languages like Python.




On 04/20/2016 10:33 AM, SPetro wrote:

Hi , but how would you write query with this, this is what I want

class Cwle(Base):
 __tablename__ = 'CardiacWaitListEntry'
 wleid = Column('WaitlistEntryID',NUMERIC(18), primary_key=True)

qr = session.query(Cwle.wleid)  <--- when I type Cwle. it will bring
intellisense with all columns (there are a lot of them :) )

select CWLE.wleid from CardiacWaitListEntry  as  CWLE


On Wednesday, April 20, 2016 at 10:25:43 AM UTC-4, SPetro wrote:

Hi, is possible to have "default" alias for table

class Cwle(Base):
 __tablename__ = 'CardiacWaitListEntry'
 __table_alias__ = 'CWLE'

I know you can do this
Cwle = aliased(Cwle, name='CWLE')

but this screw up any editor intellisense. Output of sqalchemy query
would be more readable and what we use as convention.







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


[sqlalchemy] Re: __table_alias__ option

2016-04-20 Thread SPetro
Hi , but how would you write query with this, this is what I want

class Cwle(Base):
__tablename__ = 'CardiacWaitListEntry'
wleid = Column('WaitlistEntryID',NUMERIC(18), primary_key=True)

qr = session.query(Cwle.wleid)  <--- when I type Cwle. it will bring 
intellisense with all columns (there are a lot of them :) )

select CWLE.wleid from CardiacWaitListEntry  as  CWLE


On Wednesday, April 20, 2016 at 10:25:43 AM UTC-4, SPetro wrote:
>
> Hi, is possible to have "default" alias for table
>
> class Cwle(Base):
> __tablename__ = 'CardiacWaitListEntry'
> __table_alias__ = 'CWLE'
>
> I know you can do this
> Cwle = aliased(Cwle, name='CWLE')
>
> but this screw up any editor intellisense. Output of sqalchemy query would 
> be more readable and what we use as convention.
>  
>
>
>
>
>
>
>
>

-- 
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] __table_alias__ option

2016-04-20 Thread Mike Bayer


from sqlalchemy.orm import aliased as _aliased

def aliased(entity):
return _aliased(entity, name=entity.__table_alias__)


?



On 04/20/2016 10:25 AM, SPetro wrote:

Hi, is possible to have "default" alias for table

class Cwle(Base):
 __tablename__ = 'CardiacWaitListEntry'
 __table_alias__ = 'CWLE'

I know you can do this
Cwle = aliased(Cwle, name='CWLE')

but this screw up any editor intellisense. Output of sqalchemy query
would be more readable and what we use as convention.







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


[sqlalchemy] __table_alias__ option

2016-04-20 Thread SPetro
Hi, is possible to have "default" alias for table

class Cwle(Base):
__tablename__ = 'CardiacWaitListEntry'
__table_alias__ = 'CWLE'

I know you can do this
Cwle = aliased(Cwle, name='CWLE')

but this screw up any editor intellisense. Output of sqalchemy query would 
be more readable and what we use as convention.
 







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


How to handle changing formerly-unnamed constraints (and retrying after errors in migration scripts)

2016-04-20 Thread Ben Sizer
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.

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.

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.


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

2016-04-20 Thread Piotr Dobrogost
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?


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.