Skip to content

Commit c91c3a4

Browse files
committed
Fix
1 parent a79c773 commit c91c3a4

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/backend/table_builder.rs

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

114114
if let Some(default) = default {
115-
write!(sql, " DEFAULT ").unwrap();
116-
QueryBuilder::prepare_expr(self, default, sql);
115+
// References:
116+
// https://sqlite.org/lang_createtable.html
117+
sql.write_str(" DEFAULT ").unwrap();
118+
match default {
119+
Expr::Value(_) | Expr::Constant(_) | Expr::Keyword(_) => {
120+
self.prepare_expr(default, sql)
121+
}
122+
_ => {
123+
sql.write_str("(").unwrap();
124+
self.prepare_expr(default, sql);
125+
sql.write_str(")").unwrap()
126+
}
127+
}
117128
}
118129

119130
if let Some(generated) = generated {

tests/sqlite/table.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,23 @@ fn alter_with_named_check_constraint() {
515515
r#"ALTER TABLE "glyph" ADD COLUMN "aspect" integer NOT NULL DEFAULT 101 CONSTRAINT "positive_aspect" CHECK ("aspect" > 100)"#,
516516
);
517517
}
518+
519+
#[test]
520+
fn create_default_parentheses_for_expression() {
521+
assert_eq!(
522+
Table::create()
523+
.table(Glyph::Table)
524+
.col(
525+
ColumnDef::new(Glyph::Aspect)
526+
.integer()
527+
.default(Expr::val(1).add(1)),
528+
)
529+
.to_string(SqliteQueryBuilder),
530+
[
531+
r#"CREATE TABLE "glyph" ("#,
532+
r#""aspect" integer DEFAULT (1 + 1)"#,
533+
r#")"#,
534+
]
535+
.join(" "),
536+
);
537+
}

0 commit comments

Comments
 (0)