Skip to content

Commit

Permalink
Exclude table name for DEFAULT
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Jun 3, 2020
1 parent 85d3432 commit 0ff0bee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
37 changes: 22 additions & 15 deletions clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,29 +412,36 @@ def _get_default_string(self, default, name):
)

def get_column_specification(self, column, **kw):
column_spec = super(
ClickHouseDDLCompiler, self
).get_column_specification(column, **kw)
colspec = (
self.preparer.format_column(column)
+ " "
+ self.dialect.type_compiler.process(
column.type, type_expression=column
)
)

opts = column.dialect_options['clickhouse']

if column.server_default is None:
if opts['materialized'] is not None:
column_spec += " MATERIALIZED " + self._get_default_string(
opts['materialized'], 'clickhouse_materialized'
)
elif opts['alias'] is not None:
column_spec += " ALIAS " + self._get_default_string(
opts['alias'], 'clickhouse_alias'
)
if column.server_default is not None:
colspec += " DEFAULT " + self._get_default_string(
column.server_default.arg, 'server_default'
)
elif opts['materialized'] is not None:
colspec += " MATERIALIZED " + self._get_default_string(
opts['materialized'], 'clickhouse_materialized'
)
elif opts['alias'] is not None:
colspec += " ALIAS " + self._get_default_string(
opts['alias'], 'clickhouse_alias'
)

codec = opts['codec']
if codec:
if codec is not None:
if isinstance(codec, (list, tuple)):
codec = ', '.join(codec)
column_spec += " CODEC({0})".format(codec)
colspec += " CODEC({0})".format(codec)

return column_spec
return colspec

def visit_create_column(self, create, **kw):
column = create.element
Expand Down
33 changes: 33 additions & 0 deletions tests/test_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,39 @@ def test_create_table_with_codec(self):
'ENGINE = Memory'
)

def test_create_table_column_default(self):
table = Table(
't1', self.metadata(),
Column('x', types.Int8),
Column('dt', types.DateTime, server_default=func.now()),
engines.Memory()
)

self.assertEqual(
self.compile(CreateTable(table)),
'CREATE TABLE t1 ('
'x Int8, '
'dt DateTime DEFAULT now()) '
'ENGINE = Memory'
)

def test_create_table_column_default_another_column(self):
class TestTable(get_declarative_base()):
x = Column(types.Int8, primary_key=True)
y = Column(types.Int8, server_default=x)

__table_args__ = (
engines.Memory(),
)

self.assertEqual(
self.compile(CreateTable(TestTable.__table__)),
'CREATE TABLE test_table ('
'x Int8, '
'y Int8 DEFAULT x) '
'ENGINE = Memory'
)

def test_create_table_column_materialized(self):
table = Table(
't1', self.metadata(),
Expand Down

0 comments on commit 0ff0bee

Please sign in to comment.