for this example, you need to define your mapper like this:

max_mapper = mapper( MaxRevision, mapper_sel, primary_key = [ mapper_sel.c.name ])


which has two things.  first of all, the primary key columns must be relative to the actual selectable which the mapper is defined from...once you map against a select statement, the underlying table is arbitrary...it could be composed of multiple tables, for example.  

the other thing is that a mapped class needs a primary mapper defined first.  While you can only define one primary mapper per class (which actually is per class/per entity name), you can define any number of primary mappers against a particular table or selectable...so in this case as it seems MaxRevision is just a particular view object, theres no issue creating a primary mapper for it.

On Sep 27, 2006, at 9:42 AM, Nikola Vukovljak wrote:

from sqlalchemy import *

eng = create_engine( "postgres://user:[EMAIL PROTECTED]:9999/test" )

meta = BoundMetaData( eng )

sesh = create_session( bind_to = eng )

eng.echo = True

no_populate = True

try:

    tab = Table( "sqla_arbitrary", \

                 meta, \

                 autoload = True )

except:

    tab = Table( "sqla_arbitrary", \

                 meta, \

                 Column( "id", Integer, primary_key = True ), \

                 Column( "name", String ), \

                 Column( "revision", Integer ), \

                 Column( "description", String ) )

    tab.create()

    no_populate = False

class Revision( object ):

    def __init__( self, name, desc, revision ):

        self.name = name

        self.description = desc

        self.revision = revision

    def __cmp__( self, other ):

        if self.name == other.name and \

               self.description == other.description and \

               self.revision == other.revision:

            return 0

        return 1

class MaxRevision( object ):

    def __cmp__( self, other ):

        if self.name == other.name and \

           self.revision and other.revision:

            return 0

        return 1

    def __str__( self ):

        return "# Name [%s], Revision: [%d] #" % ( self.name, self.revision )

def displayResults( title, results ):

    print "\n%s\n" % ( title )

    for r in results:

        print "%s" % ( r )

    print "\n\n"

# Create mapper

simple_mapper = mapper( Revision, tab )

# Some data

d1 = Revision( "matcher1", "My first matcher", 1 )

d2 = Revision( "matcher2", "My second matcher", 1 )

d3 = Revision( "matcher3", "My third matcher", 1 )

d4 = Revision( "matcher1", "My edited once first matcher", 2 )

d5 = Revision( "matcher1", "My editied twice first matcher", 3 )

d6 = Revision( "matcher2", "My editied one second matcher", 2 )

d7 = Revision( "matcher4", "My fourth matcher", 1 )

# Insert the data

if no_populate == False:

    sesh.save( d1 )

    sesh.save( d2 )

    sesh.save( d3 )

    sesh.save( d4 )

    sesh.save( d5 )

    sesh.save( d6 )

    sesh.save( d7 )

    sesh.flush()

# Create a select statement

sel = select( [ tab.c.name, func.max( tab.c.revision ).label( "revision" ) ], \

              group_by = [ tab.c.name ] )

results = sel.execute()

displayResults( "SELECT STATEMENT", results )

# Create an arbitrary mapper

mapper_sel = select( [ tab.c.name, func.max( tab.c.revision ).label( "revision" ) ], group_by = [ tab.c.name ] ).alias( "x" )

print "Mapper Select: [", mapper_sel , "]\n\n"

# Read only mapper

max_mapper = mapper( MaxRevision, mapper_sel, primary_key = [ tab.c["name"] ], non_primary = True )

query = sesh.query( max_mapper )

results = query.select()

displayResults( "MAPPER", results )


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to