reflect postgres default column values

Issue #43 resolved
Mike Bayer repo owner created an issue

a user has requseted that the defaults set for coluimns in postgres be pulled in as bona fide "Default" objects on the Column object, instead of the "implicit sequencing" it does now. from the thread:

Serial in fact is a an int(4) type with a default to nextval (table_column_seq):

Test=# \d Test
                                   Table "public.test"
 Column |            Type             |                     Modifiers
--------+-----------------------------+---------------------------------------------------
 id     | integer                     | not null default
nextval('test_id_seq'::regclass)

You can fetch this information with

Test=#  select column_default from information_schema.columns where
column_name='id';

          column_default
----------------------------------
 nextval('test_id_seq'::regclass)
(1 row)

Comments (3)

  1. Mike Bayer reporter

    made some progress on this one. Added a PassiveDefault object to schema, which indicates "this column has a default on it, but let the database execute it". When a table has such a default on it, the engine, upon a compiled Insert, will take note of it and have a flag "lastrow_has_defaults". The mapper then will use this flag to indicate it has to post-fetch the row, to get those defaults. i tried having the "post fetch" function in the engine, but the problem is it doesnt work with tables that dont have a primary key; even though the mapper might need to postfetch those rows, in the case that it has defined mapper-level primary key columns for that table (like with the Association object pattern). So the mapper is the one that always knows how to postfetch the row, so thats where it goes.

    Next up is modifying information_schema to produce these PassiveDefault objects from reflected table data as stated in this ticket, putting CREATE TABLE support for them in all the database modules (only postgres, untested, so far), putting more unit test stuff into test/engines.py, and also probably putting some stuff into test/objectstore.py to see if these PassiveDefaults actually work when saving objects.

  2. Mike Bayer reporter

    completed in changeset:942. postgres can reflect column defaults from information_schema. mysql/sqlite/postgres(/oracle?) support DEFAULT in schema create statements; postgres/oracle allow them to be functions as well either via literal string or via expression func.<funcname>(). unittests for create/insert/select roundtrip added.

  3. Log in to comment