Skip to content

Commit 5b37115

Browse files
committed
Reserved names replacement in tables! is also applied in joinable!
1 parent bfdddef commit 5b37115

10 files changed

Lines changed: 76 additions & 6 deletions

File tree

diesel_cli/src/infer_schema_internals/data_structures.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub struct ForeignKeyConstraint {
102102
pub child_table: TableName,
103103
pub parent_table: TableName,
104104
pub foreign_key: String,
105+
pub foreign_key_rust_name: String,
105106
pub primary_key: String,
106107
}
107108

diesel_cli/src/infer_schema_internals/inference.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ pub fn load_foreign_key_constraints(
109109

110110
constraints.map(|mut ct| {
111111
ct.sort();
112+
ct.iter_mut().for_each(|foreign_key_constraint| {
113+
if RESERVED_NAMES.contains(&foreign_key_constraint.foreign_key_rust_name.as_str()) {
114+
foreign_key_constraint.foreign_key_rust_name =
115+
format!("{}_", foreign_key_constraint.foreign_key_rust_name.as_str());
116+
}
117+
});
112118
ct
113119
})
114120
}

diesel_cli/src/infer_schema_internals/information_schema.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ where
239239
.filter(kcu::constraint_schema.eq(&foreign_key_schema))
240240
.filter(kcu::constraint_name.eq(&foreign_key_name))
241241
.select(((kcu::table_name, kcu::table_schema), kcu::column_name))
242-
.first::<(TableName, _)>(connection)?;
242+
.first::<(TableName, String)>(connection)?;
243243
let (mut primary_key_table, primary_key_column) = kcu::table
244244
.filter(kcu::constraint_schema.eq(primary_key_schema))
245245
.filter(kcu::constraint_name.eq(primary_key_name))
@@ -252,7 +252,8 @@ where
252252
Ok(ForeignKeyConstraint {
253253
child_table: foreign_key_table,
254254
parent_table: primary_key_table,
255-
foreign_key: foreign_key_column,
255+
foreign_key: foreign_key_column.clone(),
256+
foreign_key_rust_name: foreign_key_column,
256257
primary_key: primary_key_column,
257258
})
258259
},
@@ -462,12 +463,14 @@ mod tests {
462463
child_table: table_2.clone(),
463464
parent_table: table_1.clone(),
464465
foreign_key: "fk_one".into(),
466+
foreign_key_rust_name: "fk_one".into(),
465467
primary_key: "id".into(),
466468
};
467469
let fk_two = ForeignKeyConstraint {
468470
child_table: table_3.clone(),
469471
parent_table: table_2.clone(),
470472
foreign_key: "fk_two".into(),
473+
foreign_key_rust_name: "fk_two".into(),
471474
primary_key: "id".into(),
472475
};
473476
assert_eq!(

diesel_cli/src/infer_schema_internals/mysql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,18 @@ pub fn load_foreign_key_constraints(
6262
kcu::column_name,
6363
kcu::referenced_column_name,
6464
))
65-
.load::<(TableName, TableName, _, _)>(connection)?
65+
.load::<(TableName, TableName, String, _)>(connection)?
6666
.into_iter()
6767
.map(
6868
|(mut child_table, mut parent_table, foreign_key, primary_key)| {
6969
child_table.strip_schema_if_matches(&default_schema);
7070
parent_table.strip_schema_if_matches(&default_schema);
71+
7172
ForeignKeyConstraint {
7273
child_table,
7374
parent_table,
74-
foreign_key,
75+
foreign_key: foreign_key.clone(),
76+
foreign_key_rust_name: foreign_key,
7577
primary_key,
7678
}
7779
},

diesel_cli/src/infer_schema_internals/sqlite.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ pub fn load_foreign_key_constraints(
7777
ForeignKeyConstraint {
7878
child_table: child_table.clone(),
7979
parent_table,
80-
foreign_key: row.foreign_key,
80+
foreign_key: row.foreign_key.clone(),
81+
foreign_key_rust_name: row.foreign_key,
8182
primary_key: row.primary_key,
8283
}
8384
})
@@ -290,12 +291,14 @@ fn load_foreign_key_constraints_loads_foreign_keys() {
290291
child_table: table_2.clone(),
291292
parent_table: table_1.clone(),
292293
foreign_key: "fk_one".into(),
294+
foreign_key_rust_name: "fk_one".into(),
293295
primary_key: "id".into(),
294296
};
295297
let fk_two = ForeignKeyConstraint {
296298
child_table: table_3.clone(),
297299
parent_table: table_2.clone(),
298300
foreign_key: "fk_two".into(),
301+
foreign_key_rust_name: "fk_two".into(),
299302
primary_key: "id".into(),
300303
};
301304
let fks = load_foreign_key_constraints(&connection, None).unwrap();

diesel_cli/src/print_schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl<'a> Display for Joinable<'a> {
249249
write!(
250250
f,
251251
"joinable!({} -> {} ({}));",
252-
self.0.child_table.name, self.0.parent_table.name, self.0.foreign_key,
252+
self.0.child_table.name, self.0.parent_table.name, self.0.foreign_key_rust_name,
253253
)
254254
}
255255
}

diesel_cli/tests/print_schema.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ fn print_schema_with_foreign_keys() {
6464
test_print_schema("print_schema_with_foreign_keys", vec!["--with-docs"]);
6565
}
6666

67+
#[test]
68+
#[cfg(feature = "postgres")]
69+
fn print_schema_with_foreign_keys_reserved_names() {
70+
test_print_schema(
71+
"print_schema_with_foreign_keys_reserved_names",
72+
vec!["--with-docs"],
73+
);
74+
}
75+
6776
#[test]
6877
fn print_schema_column_renaming() {
6978
test_print_schema("print_schema_column_renaming", vec!["--with-docs"]);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[print_schema]
2+
file = "src/schema.rs"
3+
with_docs = true
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
table! {
2+
/// Representation of the `posts` table.
3+
///
4+
/// (Automatically generated by Diesel.)
5+
posts (id) {
6+
/// The `id` column of the `posts` table.
7+
///
8+
/// Its SQL type is `Int4`.
9+
///
10+
/// (Automatically generated by Diesel.)
11+
id -> Int4,
12+
/// The `type` column of the `posts` table.
13+
///
14+
/// Its SQL type is `Int4`.
15+
///
16+
/// (Automatically generated by Diesel.)
17+
#[sql_name = "type"]
18+
type_ -> Int4,
19+
}
20+
}
21+
22+
table! {
23+
/// Representation of the `users` table.
24+
///
25+
/// (Automatically generated by Diesel.)
26+
users (id) {
27+
/// The `id` column of the `users` table.
28+
///
29+
/// Its SQL type is `Int4`.
30+
///
31+
/// (Automatically generated by Diesel.)
32+
id -> Int4,
33+
}
34+
}
35+
36+
joinable!(posts -> users (type_));
37+
38+
allow_tables_to_appear_in_same_query!(
39+
posts,
40+
users,
41+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE users (id SERIAL PRIMARY KEY);
2+
CREATE TABLE posts (id SERIAL PRIMARY KEY, type INTEGER NOT NULL REFERENCES users);

0 commit comments

Comments
 (0)