Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 13 and Version 14 of UsingSqlAlchemy

08/21/06 09:22:46 (13 years ago)

updated to describe recent improvements in TG support of SA


  • UsingSqlAlchemy

    v13 v14  
    2727(i.e. transactions are not implicit). 
    29 This document covers the 0.1.0 release and was written on February 13, 2006. 
     29This document initially covered 0.1.0 release and was written on February 13, 2006. It was updated on August 21, 2006 for SQLAlchemy 0.2 and improved TurboGears' support. 
    3131== Getting SQLAlchemy == 
    33 SQLAlchemy made its first public release (0.1.0) on February 13, 2006. You can 
    34 get it from the cheeseshop:: 
     33You can SQLAlchemy get it from the cheeseshop: 
    3636  easy_install SQLAlchemy 
    38 Or grab it some other way via the [http://www.sqlalchemy.org/download.myt SQLAlchemy Download Page]. Michael 
    39 provides thorough [http://www.sqlalchemy.org/docs/ documentation] and the pages on python [http://www.sqlalchemy.org/docs/sqlconstruction.myt sql construction], 
     39Or grab it some other way via the [http://www.sqlalchemy.org/download.myt SQLAlchemy Download Page]. Michael provides thorough [http://www.sqlalchemy.org/docs/ documentation] and the pages on python [http://www.sqlalchemy.org/docs/sqlconstruction.myt sql construction], 
    4040[http://www.sqlalchemy.org/docs/datamapping.myt basic data mapping], and [http://www.sqlalchemy.org/docs/adv_datamapping.myt advanced data mapping] are well worth the read. 
    4848the main SQLAlchemy docs. This document only covers points relevant to TG. 
    50 == Configuration == 
     50== Getting started with SQLAlchemy == 
    52 This section describes how to set up SQLAlchemy so that it will use a seperate 
    53 config in development and production mode. 
     52The simplest route is to quickstart new project with --sqlalchemy switch. This will put appropriate for SQLAlchemy code for your model.py and sample SQLAlchemy uris in configuration files. If you have a project at hand which you want to migrate to SQLAlchemy I still suggest quickstart new project and then incorporate relevant pieces into your code. 
    55 The first thing you need to be aware of when coming from SQLObject is that 
    56 dburis in SQLAlchemy have schemas that vary based on the backend. Yes, this 
    57 entirely defeats the purpose of having them in uri format. Pick your format 
    58 from one of the uris shown below:: 
     54After you edited sqlalchemy.dburi to point to valid database connection you need to run 'tg-admin sql create' to init database schema. Unlike SQLObject, identity tables are not created automatically (at least, not yet). 
    60  * !sqlite://filename=querytest.db 
    61  * !sqlite://filename=:memory: 
    62  * !postgres://database=test&host=localhost&user=scott&password=tiger 
    63  * !mysql://db=test&host=localhost&user=scott&passwd=tiger 
    64  * !mysql://db=test&unix_socket=/tmp/foo.socket&user=scott&passwd=tiger 
    65  * !oracle://dsn=mydsn&user=scott&passwd=tiger 
     56== SQLAlchemy in TG == 
    67 And insert the following into your ``devcfg.py`` and ``prodcfg.py`` files:: 
    68 {{{ 
    69 #!python 
    70   sqlalchemy.dburi = *<your connection uri>* 
    71   sqlalchemy.echo = 0                        #Set to 1 for echo 
    72 }}} 
    73 Now flip over to your ``<project>/model.py`` file and replace the contents 
    74 with: 
    75 {{{ 
    76 #!python 
    77   from sqlalchemy import * 
    78   import cherrypy 
     58After reading SQLAlchemy docs you need to know how it maps to TurboGears. 
    80   engine = create_engine(cherrypy.config.get('sqlalchemy.dburi'), 
    81                          echo=cherrypy.config.get('sqlalchemy.echo',0)) 
    82 }}} 
     60Here it goes, taken from [http://groups.google.com/group/turbogears/msg/1c818ff704463f84 this thread]:  
    84 With those lines, you're ready to go. Keep everything SQLAlchemy related below 
    85 the engine declaration. 
     62 * use the predefined "metadata" and "session" (from turbogears.database import metadata, session) 
     63 * The context is "session.context" 
     64 * Use the activemapper layer if requirements are simple 
     65 * Use assign_mapper if activemapper is not sufficient to handle the requirements, or there is a  liking to separate table definitions etc. from business logic  
    8767== Tips and Gotchas ==