Re: [sqlalchemy] Set/clear mapping for some tables only

2016-12-09 Thread Srikanth Bemineni
Hi,

Sorry for the delay. Marking the DBsession dirty after a non ORM operation 
works for now.

Zope.sqlalchemy.mark_changed(DBSession())

Thanks for the info Simon

Srikanth Bemineni

On Tuesday, November 29, 2016 at 9:10:32 AM UTC-6, Mike Bayer wrote:
>
> Hi Srikanth, answers inline. 
>
> On 11/28/2016 10:04 PM, Srikanth Bemineni wrote: 
> > Hi Mike, 
> > 
> > I am using pyramid_tm as transaction manager which is part of the 
> > pyramid framework. Is this the third party transaction manager that you 
> > are mentioning about ?. 
>
> That, as well as zope.transaction, yes. 
>
>
>   Can you please tell us how pyramid_tm, 
> > ZopeTransacationExtension and sqlachemy come together. 
>
> I'm not familiar with how pyramid_tm and ZopeTransactionExtension 
> interact specifically, you'd want to talk to the Pyramid list for that 
> (or Simon here seems to know more about it than me). 
> ZopeTransactionExtension makes use of public event hooks within the 
> SQLAlchemy Session to take control of the scope of a database 
> transaction, it mediates the SQL database transaction management of 
> SQLAlchemy Session with the more generic "transaction" of Pyramid.  That 
> is, SQLAlchemy's transaction becomes an implementation detail. 
>
>
> Why are they so 
> > many layers for a simple transactions.?. 
>
> This is a design decision made by the Pyramid framework.  The 
> transactional concept in Pyramid encompasses many more aspects than just 
> SQL database transactions, and can join together the SQL database 
> transaction with lots of other things like other kinds of datastores, 
> sending emails, things like that. 
>
> The Pyramid devs will also tell you that usage of pyramid_tm and all of 
> that is entirely optional in Pyramid, although they are common.  Other 
> frameworks such as Flask have a much thinner interaction layer in the 
> area of transactions as well. 
>
>
>   When they are  so many 
> > libraries involved, may be I need to post this question in multiple 
> > forums to get all the answers?. 
>
> I would stress that you as the developer are free to choose which 
> interaction layers you want to use and which are not necessary.   Using 
> more layers does mean you have more of an up-front learning curve, with 
> the benefit being potentially cleaner design once your application has 
> shown it makes use of these features. 
>
>
>
> If there are any pyramid experts please 
> > let me know what is  happening here ? 
> > 
> > http://docs.pylonsproject.org/projects/pyramid_tm/en/latest/ 
> > 
> > with session_shardid(DBSession,table_hash): 
> > Base.metadata.create_all(DBSession.connection()) 
> > self.request.tm.commit()  -> This also doesn't help all the statements 
> > just roll back. 
> > 
> > Srikanth Bemineni 
> > 
> > 
> > 
> > 
> > On Monday, November 28, 2016 at 8:28:22 AM UTC-6, Mike Bayer wrote: 
> > 
> > 
> > 
> > On 11/27/2016 09:24 AM, Srikanth Bemineni wrote: 
> > > Hi Mike, 
> > > 
> > > I even tried flushing and committing the transaction. But still 
> > the same 
> > > issue. I was trying to figure out why is getting rolled back. 
> > Please see 
> > > the below stack trace 
> > 
> > 
> > that rollback is normal when the connection is returned to the 
> > connection pool.  you are still not getting your Session committed. 
> > Because you are using ZopeTransactionExtension it probably is 
> managing 
> > the transaction on the Connection externally to the Session.   You 
> need 
> > to use your chosen third party transaction commit facilities here to 
> > ensure all transactions are committed (or don't use that tool if it 
> is 
> > making things more difficult). 
> > 
> > Alternatively, for the CREATE you can metadata.create_all() on the 
> > connection: 
> > 
> > 
> > with engine.connect() as conn: 
> >  conn.info ['whatever token you need'] = 'the 
> > token' 
> >  metadata.create_all(conn) 
> > 
> > 
> > 
> > 
> > 
> > > 
> > > 
> > > | 
> > > 
> > > 
> > > 
> > 
> DBSession=scoped_session(sessionmaker(extension=ZopeTransactionExtension())) 
>
> > 
> > > 
> > > 
> > > 
> > > withsession_shardid(DBSession,table_hash): 
> > >  Base.metadata.create_all(DBSession.connection()) 
> > >  DBSession.flush() 
> > >  transaction.commit() 
> > > 
> > > 
> > > Wasanalyzing why it isgetting rolled back.Startedwitha trace. 
> > > 
> > > 
> > > Traceback(most recent call last): 
> > > 
> > > 
> > 
> File"/home/izero/devel/medics_city/medics_city_env/lib/python3.4/site-packages/sqlalchemy/pool.py",line
>  
>
> > 
> > > 687,in_finalize_fairy 
> > > fairy._reset(pool) 
> > > 
> > > 
> > 
> File"/home/izero/devel/medics_city/medics_city_env/lib/python3.4/site-packages/sqlalchemy/pool.py",line
>  
>
> > 
> > > 827,in_reset 
> > > self._reset_agent.rollback() 
> > > 
> 

Re: [sqlalchemy] `session_factory` equivalent for regular sessions (non- scoped_session ) ?

2016-12-09 Thread Jonathan Vanasco
I'm transitioning the code to plain sessions.

I can attach the sessionmaker onto to the session at generation – that is a 
backup plan right now.  I'm just wondering if there's a more correct way

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


Re: [sqlalchemy] `session_factory` equivalent for regular sessions (non- scoped_session ) ?

2016-12-09 Thread mike bayer



On 12/09/2016 04:18 PM, Jonathan Vanasco wrote:

I ran into an issue when transitioning a scoped session to a regular
session in an application.

A block of code had previously created a secondary session (to the same
database) to autocommit some data outside of the session/transaction:

dbSessionAutocommit = dbSession.session_factory(autocommit = True)
   ... do something ...
dbSessionAutocommit.flush()
dbSessionAutocommit.close()

this broke on transition, as the non-scoped session doesn't have a
`session_factory` method.

going through the documentation, there's nothing in the
API/narrative/faq that seems to correspond with my needs (new session,
same engine, different connection; only different is in autocommit)

does anyone have a clue on how I could build a new session?
dbSession.__class__(autocommit=True) won't work, as it won't have a
configured engine.


you have just a plain Session?   why can't you carry along the 
sessionmaker from your scoped session ?   the point of sessionmaker is 
to hold onto configuration options.





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


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


[sqlalchemy] `session_factory` equivalent for regular sessions (non- scoped_session ) ?

2016-12-09 Thread Jonathan Vanasco
I ran into an issue when transitioning a scoped session to a regular 
session in an application.

A block of code had previously created a secondary session (to the same 
database) to autocommit some data outside of the session/transaction:
   
dbSessionAutocommit = dbSession.session_factory(autocommit = True)
   ... do something ...
dbSessionAutocommit.flush()
dbSessionAutocommit.close()

this broke on transition, as the non-scoped session doesn't have a 
`session_factory` method.

going through the documentation, there's nothing in the API/narrative/faq 
that seems to correspond with my needs (new session, same engine, different 
connection; only different is in autocommit)

does anyone have a clue on how I could build a new session? 
dbSession.__class__(autocommit=True) won't work, as it won't have a 
configured engine.

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


Re: [sqlalchemy] declarative Association table w/ foreign_keys in relationship() still raises "multiple foreign key paths linking the tables"

2016-12-09 Thread bkc
That's a good suggestion, thanks.

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


Re: [sqlalchemy] declarative Association table w/ foreign_keys in relationship() still raises "multiple foreign key paths linking the tables"

2016-12-09 Thread mike bayer

leader/follower is often used in this case.


On 12/09/2016 10:33 AM, b...@sfi.ca wrote:

Thanks Mike for the quick reply.

I have changed the code so it uses backref on one side. The models now
get created w/o error. I haven't actually tested using the relationship
but I'm sure it's fine now.

For anyone else with this problem, here's the 'fixed' code. I didn't
change the Artifact_Reference table, it's the same as posted above. I'm
not keen on the naming master/slave.. maybe primary/secondary would be
better.. It's not exactly Parent/Child.. heh.


|
class Artifact_Relation(Base):
__tablename__ = 'artifact_relation'
master_artifact_id = Column(
Integer,
ForeignKey('artifact.id', name='artifact_relation_master_id_fk',
ondelete="cascade", onupdate="cascade"),
primary_key=True,
nullable=False
)

slave_artifact_id = Column(
Integer,
ForeignKey('artifact.id', name='artifact_relation_slave_id_fk',
ondelete="cascade", onupdate="cascade"),
primary_key=True,
nullable=False
)

relationship_type = Column(String(24), nullable=False)

slave = relationship("Artifact", backref=backref("masters"),
foreign_keys=[slave_artifact_id])
master = relationship("Artifact", backref=backref("slaves"),
foreign_keys=[master_artifact_id])

class Artifact(Base):
"Artifact"
__tablename__ = 'artifact'
id = Column('id', Integer, primary_key=True)
artifact_type = Column('artifact_type', String(16), nullable=False)
__mapper_args__ = {
'polymorphic_on': artifact_type
}

artifact_references = relationship(
'Artifact_Reference',
back_populates='artifact',
cascade="all, delete-orphan",
passive_deletes=True
)

|


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


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


Re: [sqlalchemy] declarative Association table w/ foreign_keys in relationship() still raises "multiple foreign key paths linking the tables"

2016-12-09 Thread bkc
Thanks Mike for the quick reply.

I have changed the code so it uses backref on one side. The models now get 
created w/o error. I haven't actually tested using the relationship but I'm 
sure it's fine now.

For anyone else with this problem, here's the 'fixed' code. I didn't change 
the Artifact_Reference table, it's the same as posted above. I'm not keen 
on the naming master/slave.. maybe primary/secondary would be better.. It's 
not exactly Parent/Child.. heh.


class Artifact_Relation(Base):
__tablename__ = 'artifact_relation'
master_artifact_id = Column(
Integer,
ForeignKey('artifact.id', name='artifact_relation_master_id_fk', 
ondelete="cascade", onupdate="cascade"),
primary_key=True,
nullable=False
)

slave_artifact_id = Column(
Integer,
ForeignKey('artifact.id', name='artifact_relation_slave_id_fk', 
ondelete="cascade", onupdate="cascade"),
primary_key=True,
nullable=False
)

relationship_type = Column(String(24), nullable=False)

slave = relationship("Artifact", backref=backref("masters"), 
foreign_keys=[slave_artifact_id])
master = relationship("Artifact", backref=backref("slaves"), 
foreign_keys=[master_artifact_id])

class Artifact(Base):
"Artifact"
__tablename__ = 'artifact'
id = Column('id', Integer, primary_key=True)
artifact_type = Column('artifact_type', String(16), nullable=False)
__mapper_args__ = {
'polymorphic_on': artifact_type
}

artifact_references = relationship(
'Artifact_Reference',
back_populates='artifact',
cascade="all, delete-orphan",
passive_deletes=True
)



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