Skip to content

SQLite reflection: parentheses wrongly stripped from expression in default clause #579

@zmwangx

Description

@zmwangx

I have a table (SQLite) with a column that looks like

created_at DATETIME DEFAULT (datetime('now', 'localtime')) NOT NULL,

The parentheses around datetime('now', 'localtime') are syntactically necessary, without which one would get a syntax error:

The DEFAULT clause specifies a default value to use for the column if no value is explicitly provided by the user when doing an INSERT. If there is no explicit DEFAULT clause attached to a column definition, then the default value of the column is NULL. An explicit DEFAULT clause may specify that the default value is NULL, a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses.

https://www.sqlite.org/lang_createtable.html (emphasis mine)

When I use op.batch_alter_table, the reflected column's server_default is a text clause datetime('now', 'localtime'), without parentheses, so when alembic attempts to create the new table it fails:

CREATE TABLE _alembic_tmp_foo (
    ...
	created_at DATETIME DEFAULT datetime('now', 'localtime') NOT NULL, -- syntax error
)

I'm new to SQLA and Alembic so please excuse me if the fault is on me.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions