[sqlalchemy] Best practice for binding the engine

2014-03-09 Thread Bao Niu
From reading the documentation I learned that you can either bind an engine 
to a session:
engine = create_engine('sqlite:///{}'.format(dbPath), echo=False)
Session = sessionmaker(bind=engine)

 or to a declarative_base:
engine = create_engine('sqlite:///{}'.format(dbPath), echo=False)
Sqlalchemy_base = declarative_base(engine)

Is there a best practice like always binding to a session?
I currently bind the engine to both:
engine = create_engine('sqlite:///{}'.format(dbPath), echo=False)
Session = sessionmaker(bind=engine)
Sqlalchemy_base = declarative_base(engine)
From running my codes I didn't encounter any problem. But I wonder how 
SqlAlchemy resolves the conflict internally if there are two bindings? Is 
there some potential problems down the road if I kept using this manner? 
Thanks.

-- 
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] databases and asynchronous programming

2014-03-09 Thread Chris Withers

Hi All,

So, one of the projects I'm playing with at the moment is a big ball of 
asynchronous networking (tonnes of protocols, some tcp, some multicast) 
which I want to stick a webapi onto (normal requests + websocket) and 
probably do some database interaction with.


So, aside from figuring out which async path to go (twisted, tornado, 
tulip or good old fashioned asyncore), I have some concerns about 
database interaction.


First question: I'm guessing I should do all database access in a 
spawned off thread, lest it blocks?


Second question: if I end up using the ORM, I guess a safe approach 
would be to expunge the objects from the session before I return them 
from the thread that does the db interaction?


Any other advice people have in this area?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing  Python Consulting
- http://www.simplistix.co.uk

--
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] Cascade Delete, session confusion

2014-03-09 Thread Dmitry Berman
Hi all,

I just started using SQLA, and I am confused by some cascade delete 
behaviour I am seeing. Please see the code and tests below which show that 
the session is seeing table rows in one area and not in the other:

CODE:

print Product1Mod3
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
product_name = Column(String(250), unique=True)
vendor_id = Column(Integer, ForeignKey('vendors.id'), nullable=False)

vendor = relationship('Vendor', backref = backref('products', 
order_by=id, cascade=all, delete-orphan))

def __init__(self, product_name, vendor_id):
self.product_name = product_name
self.vendor_id = vendor_id

def __repr__(self):
return 'Product: %r Product ID: %r Vendor ID: %r' % 
(self.product_name, self.id, self.vendor_id)


class Module(Base):
__tablename__ = 'modules'
id = Column(Integer, primary_key=True)
module_name = Column(String(250), unique=True)
product_id = Column(Integer, ForeignKey('products.id'), nullable=False)

product = relationship('Product', backref = backref('modules', 
order_by=id, cascade=all, delete-orphan))

def __init__(self, module_name, product_id):
self.module_name = module_name
self.product_id = product_id


def __repr__(self):
return 'Module: %r Module ID: %r Product ID: %r' % 
(self.module_name, self.id ,self.product_id)

TESTING:

msg('Module Tests')
Product2Mod1 = Module('Product2Mod1',1)
Product2Mod2 = Module('Product2Mod2',1)
Product1Mod1 = Module('Product1Mod1',2)
Product1Mod2 = Module('Product1Mod2',2)
Product1Mod3 = Module('Product1Mod3',2)
db_session.add(Product2Mod1)
db_session.add(Product2Mod2)
db_session.add(Product1Mod1)
db_session.add(Product1Mod2)
db_session.add(Product1Mod3)
db_session.commit()
msg(Product2Mod1 Product:)
print Product2Mod1.product

msg('delete tests')

print Query to show all products: \n
print Product.query.all()

print \nQuery to show all modules: \n
print Module.query.all()

print \ndeleting product 1: db_session.delete(Product1) -- 
db_session.commit()
db_session.delete(Product1)
db_session.commit()

print \nQuery to check for changes with products and modules, shows that 
the modules and product are gone:\n
print Product.query.all()
print Module.query.all()

print \nThe modules below belong to the deleted product, they should have 
disappeared, but do not: -- NOT SURE WHY THIS IS HAPPENING
print Product1Mod1
print Product1Mod2
print Product1Mod3

-- 
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: Cascade Delete, session confusion

2014-03-09 Thread Dmitry Berman
I also looked at this through SQLite database browser, and the database is 
correct, so is this a Python side error?

-- 
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: databases and asynchronous programming

2014-03-09 Thread Jonathan Vanasco
I'm using twisted and looking to move raw SQL ( via twisted's 
`runInteraction` ) into my SqlAlchemy model.  I've only played with it a 
bit.  

A few notes:  

Under Twisted you can't have threads spawned threads. Your computer will 
want to die.   I did and crashed the server several times, daily.  My app 
originally had some work done in deferToThread(), which then spawned off 
some DB specific stuff in the `runInteraction`.  Every 3-6hours the app 
would crash and restart.  The fix I chose was to do everything in the 
runInteraction (I lost the deferToThread layer).  Conceivably, one could 
use deferToThread instead of runInteraction, and then run SqlAlchemy in 
there directly.

Someone posted a few weeks ago about using one of the extra arguments to 
ScopedSession to get it running with Twisted.  

-- 
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] Cascade Delete, session confusion

2014-03-09 Thread Michael Bayer

On Mar 9, 2014, at 3:18 PM, Dmitry Berman dmikha...@gmail.com wrote:

 
 print \nQuery to check for changes with products and modules, shows that the 
 modules and product are gone:\n
 print Product.query.all()
 print Module.query.all()
 
 print \nThe modules below belong to the deleted product, they should have 
 disappeared, but do not: -- NOT SURE WHY THIS IS HAPPENING
 print Product1Mod1
 print Product1Mod2
 print Product1Mod3


not sure what you're expecting there, are you expecting that the Product1Mod1 
symbol would be modified within your interpreter to be None?  Python can't do 
that under normal circumstances.  A variable always points to the thing 
that it was assigned to, there's no (normal, non-hacky) mechanism by which 
variables change into None without explicitly being reassigned.

Those product objects represent what used to be in those rows.  They have a 
deleted flag you can see:

 from sqlalchemy import inspect
 inspect(deleted_product).deleted
True

-- 
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] tracking history across a 'flush()'

2014-03-09 Thread Jonathan Vanasco
I have some lightweight revision tracking on some models.  I generate a 
diff based on the history of the object (via inspector).

a limitation I just realized, is that this history only dates back to the 
most recent flush() -- it doesn't date back to the initial load.

are there any existing facilities that would let me access the originally 
loaded attributes -- prior to any flushes ?

thankfully i don't need this functionality, my code works fine as-is. i'd 
just like to be safe and use anything better if available.

-- 
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] Cascade Delete, session confusion

2014-03-09 Thread Dmitry Berman
This makes a lot of sense, I just didn't realize how it worked... 

*When I did the following:*
print inspect(Product1Mod1).deleted
print inspect(Product1Mod2).deleted
print inspect(Product1Mod3).deleted

*Instead of just:*
print Product1Mod1 
print Product1Mod2 
print Product1Mod3 

This returned:
True
True
True

And this makes total sense now. Thanks.


On Sunday, March 9, 2014 7:22:49 PM UTC-4, Michael Bayer wrote:


 On Mar 9, 2014, at 3:18 PM, Dmitry Berman dmik...@gmail.com javascript: 
 wrote: 

  
  print \nQuery to check for changes with products and modules, shows 
 that the modules and product are gone:\n 
  print Product.query.all() 
  print Module.query.all() 
  
  print \nThe modules below belong to the deleted product, they should 
 have disappeared, but do not: -- NOT SURE WHY THIS IS HAPPENING 
  print Product1Mod1 
  print Product1Mod2 
  print Product1Mod3 


 not sure what you’re expecting there, are you expecting that the 
 “Product1Mod1” symbol would be modified within your interpreter to be None? 
  Python can’t do that under print inspect(Product1Mod1).deleted
 print inspect(Product1Mod2).deleted
 print inspect(Product1Mod3).deletednormal circumstances.  A variable 
 always points to the thing that it was assigned to, there’s no (normal, 
 non-hacky) mechanism by which variables change into “None” without 
 explicitly being reassigned. 

 Those product objects represent what used to be in those rows.  They have 
 a “deleted” flag you can see: 

  from sqlalchemy import inspect 
  inspect(deleted_product).deleted 
 True 



-- 
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] [ANN] SQLTap - SQL profiling and introspection for SQLAlchemy applications

2014-03-09 Thread Alan Shreve
A few years ago, I built SQLTap, a simple library to hook into SQLAlchemy and 
pull out statistics and information on the queries you ran. This last couple 
days I've overhauled it an updated it to make it more useful!

You basically just start the profiler and then it can dump out nice browsable 
HTML reports like this:

http://sqltap.inconshreveable.com/_images/sqltap-report-example.png

SQLTap helps you answer questions like:

What queries is my application running?
How long do they take on average? At maximum? At minimum? At median?
What sequences of function calls lead to each invocation of a query? Where in 
my source code is the query executed?

The latest updates and improvements make up sqltap version 0.3 and include:

- The report now has a sexy new HTML UI based on bootstrap3.
- You can add sqltap to any WSGI application and get a live, updating dashboard 
of all the queries going through an application.
- The WSGI integration has controls to enable/disable the profiling at any time 
so you can even include it in production applications for selective profiling.
- The library's API is now greatly improved, allowing you to do your own 
real-time collection of statistics as well as allowing you to create individual 
profilers for different engines instead of forcing a global profiler.
- Updated/improved documentation

Install with:

pip install sqltap

And in your code:

import sqltap
profiler = sqltap.start()

# sometime later after queries have been run
sqltap.report(profiler.collect(), report.html)

Links for reference:
Code and some documentation (star it!): 
https://github.com/inconshreveable/sqltap
Documentation: http://sqltap.inconshreveable.com/
On PyPI: https://pypi.python.org/pypi/sqltap

Enjoy!

- alan

-- 
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] tracking history across a 'flush()'

2014-03-09 Thread Michael Bayer

On Mar 9, 2014, at 8:25 PM, Jonathan Vanasco jonat...@findmeon.com wrote:

 I have some lightweight revision tracking on some models.  I generate a diff 
 based on the history of the object (via inspector).
 
 a limitation I just realized, is that this history only dates back to the 
 most recent flush() -- it doesn't date back to the initial load.
 
 are there any existing facilities that would let me access the originally 
 loaded attributes -- prior to any flushes ?
 
 thankfully i don't need this functionality, my code works fine as-is. i'd 
 just like to be safe and use anything better if available.


pretty much just rollback() is what we have there :).  a long time ago I looked 
into seeing if the attributes could actually store multiple savepoints and have 
rollback built in and all that, and determined it would be enormously 
complicated and have a lot of nasty edge cases too.

-- 
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] [ANN] SQLTap - SQL profiling and introspection for SQLAlchemy applications

2014-03-09 Thread Michael Bayer
hey that looks pretty nice, ill give it a shoutout.


On Mar 9, 2014, at 9:21 PM, Alan Shreve a...@inconshreveable.com wrote:

 A few years ago, I built SQLTap, a simple library to hook into SQLAlchemy and 
 pull out statistics and information on the queries you ran. This last couple 
 days I've overhauled it an updated it to make it more useful!
 
 You basically just start the profiler and then it can dump out nice browsable 
 HTML reports like this:
 
 http://sqltap.inconshreveable.com/_images/sqltap-report-example.png
 
 SQLTap helps you answer questions like:
 
 What queries is my application running?
 How long do they take on average? At maximum? At minimum? At median?
 What sequences of function calls lead to each invocation of a query? Where in 
 my source code is the query executed?
 
 The latest updates and improvements make up sqltap version 0.3 and include:
 
 - The report now has a sexy new HTML UI based on bootstrap3.
 - You can add sqltap to any WSGI application and get a live, updating 
 dashboard of all the queries going through an application.
 - The WSGI integration has controls to enable/disable the profiling at any 
 time so you can even include it in production applications for selective 
 profiling.
 - The library's API is now greatly improved, allowing you to do your own 
 real-time collection of statistics as well as allowing you to create 
 individual profilers for different engines instead of forcing a global 
 profiler.
 - Updated/improved documentation
 
 Install with:
 
 pip install sqltap
 
 And in your code:
 
 import sqltap
 profiler = sqltap.start()
 
 # sometime later after queries have been run
 sqltap.report(profiler.collect(), report.html)
 
 Links for reference:
 Code and some documentation (star it!): 
 https://github.com/inconshreveable/sqltap
 Documentation: http://sqltap.inconshreveable.com/
 On PyPI: https://pypi.python.org/pypi/sqltap
 
 Enjoy!
 
 - alan
 
 -- 
 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.