I found that the following works.  I'm wondering if there's a better way.

    def _add_insert_default_values(self, kwargs):
        for col in self._table.c.keys():
            default = self._table.c[col].default
            if default is not None and default.is_scalar:
                if col not in kwargs:
                    kwargs[col] = default.arg

On Fri, Nov 4, 2011 at 3:19 PM, Roger Hoover <roger.hoo...@gmail.com> wrote:

> Hi,
>
> I'm trying to use the sqlalchemy compiler to generate insert statements
> that I can feed myself to MySQLdb.  With a sqlalchemy table definition, I'm
> able to get a nice SQL statement using the following.
>
> query =
> str(table.insert().values(**kwargs).compile(dialect=MySQLDialect(paramstyle='pyformat')))
>
> However, if the table has literal default values for any of the columns,
> those column names end up in the generated SQL statement as variables even
> if kwargs does not contain that key .   How can I get a dict of the literal
> keys to merge with ones passed in when I execute the query?
>
> Example:
>
> Table('fubar', metadata,
>             Column('id', Integer, primary_key=True),
>             Column('status_id', Integer, nullable=False, default=1),
>             Column('modified', DateTime, default=func.current_timestamp(),
> onupdate=func.current_timestamp()),
>             Column('created', DateTime, default=func.current_timestamp()),
>             mysql_engine='InnoDB'
>         )
>
> produces
>
> INSERT INTO fubar (status_id, modified, created) VALUES (%(status_id)s,
> CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
>
> I need to get a dict of literal defaults from somewhere so that I can pass
> in {'status_id': 1} to cursor.execute()
>
> Thanks,
>
> Roger
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to