Skip to content

Commit a2d5b0c

Browse files
committed
Fix single-column normalization
1 parent b637927 commit a2d5b0c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/main.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,13 @@ fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Se
262262
let mut subtable: Option<Table> = match col["cols"].is_badvalue() {
263263
true => None,
264264
false => {
265-
if norm.is_some() && col["file"].is_badvalue() { // Normalized subtable; writes its primary key into the parent table
265+
if norm.is_some() && col["file"].is_badvalue() { // Many-to-one relation (subtable with fkey in parent table)
266266
let mut subtable = add_table(colname, &path, norm, settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), None);
267267
subtable.normalized = true;
268268
Some(subtable)
269269
}
270-
else {
270+
else { // If norm.is_some(): many-to-many relation (this file will contain the crosslink table)
271+
// Otherwise: one-to-many relation (this file will contain the subtable with the parent table fkey)
271272
let filename = col["file"].as_str().unwrap_or_else(|| fatalerr!("Error: subtable {} has no 'file' entry", colname));
272273
if table.columns.is_empty() { fatalerr!("Error: table '{}' cannot have a subtable as first column", name); }
273274
Some(add_table(colname, &path, Some(filename), settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), Some(format!("{} {}", name, table.columns[0].datatype))))
@@ -286,9 +287,10 @@ fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Se
286287
let domain = match norm {
287288
Some(filename) => {
288289
if filename == "true" { fatalerr!("Error: 'norm' option now takes a file path instead of a boolean"); }
289-
let file = match col["file"].is_badvalue() {
290-
true => None, // One-to-many relation
291-
false => Some(filename) // Many-to-many relation
290+
let file = match subtable {
291+
Some(_) if col["file"].is_badvalue() => None, // Many-to-one relation (subtable with fkey in parent table)
292+
Some(_) => Some(filename), // Many-to-many relation (subtable with crosslink table)
293+
None => Some(filename) // Many-to-one relation (single column) with auto serial
292294
};
293295
let mut domain = Domain::new(colname, colpath, file, settings);
294296
domain.table.columns.push(Column { name: String::from("id"), path: String::new(), datatype: String::from("integer"), ..Default::default()});

0 commit comments

Comments
 (0)