Skip to content

Commit c247ca5

Browse files
committed
Fix
1 parent a79c773 commit c247ca5

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/backend/table_builder.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,25 @@ pub trait TableBuilder:
112112
}
113113

114114
if let Some(default) = default {
115-
write!(sql, " DEFAULT ").unwrap();
116-
QueryBuilder::prepare_expr(self, default, sql);
115+
// Wrap expressions in parentheses.
116+
// Most of database backends support this syntax.
117+
//
118+
// In MySQL 5.7, the DEFAULT clause doesn't accept any expressions,
119+
// so it will be invalid SQL in any case.
120+
//
121+
// References:
122+
// https://sqlite.org/lang_createtable.html
123+
sql.write_str(" DEFAULT ").unwrap();
124+
match default {
125+
Expr::Value(_) | Expr::Constant(_) | Expr::Keyword(_) => {
126+
self.prepare_expr(default, sql)
127+
}
128+
_ => {
129+
sql.write_str("(").unwrap();
130+
self.prepare_expr(default, sql);
131+
sql.write_str(")").unwrap()
132+
}
133+
}
117134
}
118135

119136
if let Some(generated) = generated {

0 commit comments

Comments
 (0)