Hi all,

I have a PG database with tables like the following

 Column  |            Type             |                Modifiers               
  
---------+-----------------------------+------------------------------------------
 id      | uuid                        | not null default uuid_generate_v1()
 created | timestamp without time zone | not null default 
transaction_timestamp()
 changed | timestamp without time zone | 
 name    | character varying(192)      | not null
 code    | character varying(64)       | not null
 value   | integer                     | 

with a trigger that updates the "changed" field whenever a record is modified.

Accordingly to the documentation[1] I thus modelled it like in the following
way:

    import sqlalchemy as sa
    import sqlalchemy.orm as orm
    import sqlalchemy.types as types
    import sqlalchemy.dialects.postgresql as sapg


    md = sa.MetaData()


    testtable = sa.Table(
        'testtable', md,
        sa.Column('id', sapg.UUID(True),
                  primary_key=True,
                  nullable=False,
                  server_default=sa.func.uuid_generate_v1()),
        sa.Column('created', types.DateTime(),
                  nullable=False,
                  server_default=sa.func.transaction_timestamp()),
        sa.Column('changed', types.DateTime(),
                  server_onupdate=sa.FetchedValue()),
        sa.Column('name', types.Unicode(192),
                  nullable=False),
        sa.Column('code', types.Unicode(64),
                  nullable=False),
        sa.Column('value', types.Integer,
                  nullable=True))


    class Base:
        def __init__(self, **kwargs):
            cls_ = type(self)
            for k in kwargs:
                setattr(self, k, kwargs[k])


    class TestTable(Base):
        pass


    orm.mapper(TestTable, testtable)


This works, but I notice that SA explicitly inserts the "changed" field with a
None value on INSERTs, that is the following snippet:

    e = sa.create_engine('postgresql://localhost/test', echo=True)
    md.create_all(e)

    s = orm.Session(e)

    o1 = TestTable(name='foo', code='bar')
    s.add(o1)
    s.commit()

produces a log like the following:

    2018-02-04 11:34:12,464 INFO sqlalchemy.engine.base.Engine INSERT INTO 
testtable (changed, name, code, value) VALUES (%(changed)s, %(name)s, %(code)s, 
%(value)s) RETURNING testtable.id
    2018-02-04 11:34:12,464 INFO sqlalchemy.engine.base.Engine {'changed': 
None, 'name': 'foo', 'code': 'bar', 'value': None}

The doubt is: why is SA providing explicit None values for both "changed" and
"value" fields? Am I missing something in the table description or what?

Thanks in advance for any enlightenment!

ciao, lele.

[1] http://docs.sqlalchemy.org/en/latest/core/defaults.html#triggered-columns
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  |                 -- Fortunato Depero, 1929.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to