I have added support for default column values, which builds upon the same mechanism that does sequences:

def mydefault():
        """a function that returns a new integer ID"""
        return mycounter.increment()

 t = Table('default_test1', db,
        # set a default function
        Column('col1', Integer, primary_key=True, default=mydefault),

        # set a literal default
        Column('col2', String(20), default="imthedefault"),

        # any ClauseElement, which will be pre-executed
        Column('col3', Integer, default=func.count(1)),
        )

when you do an insert:

        t.insert().execute()

you get (positional params shown here), assuming mydefault() returns 27:

        SELECT count(?)
        [1]
        INSERT INTO default_test1 (col1, col2, col3) VALUES (?, ?, ?)
        [27, 'imthedefault', 1]

This default functionality applies to all inserts for the table, whether your making your own inserts or using the mappers.

If you are using mappers, there is also another way to set up default values on an object when its created, at the object level via MapperExtension:

class MyExt(MapperExtension):
        def before_insert(self, mapper, instance):
                instance.main_id = 10
                instance.category_id = 17

mapper = mapper(MyClass, mytable, extension=MyExt())

And the "before_insert" method will be called for each new MyClass instance right before its saved.

MapperExtension actually has a bunch of other hooks too with more on the way. I still need to write full docs for MapperExtension.




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to