Fix doubling tables in n-ary relationships with 0,1 / 1,1
[erdiag.git] / parser.js
index 41eff93..6d028fd 100644 (file)
--- a/parser.js
+++ b/parser.js
@@ -89,12 +89,12 @@ class ErDiags
        // attributes: ArrayOf {name, [isKey], [type], [qualifiers]}
        parseAttributes(lines, start, end)
        {
-               let attributes = [];
+               let attributes = [ ];
                for (let i=start; i<end; i++)
                {
                        let field = { };
                        let line = lines[i];
-                       if (line.charAt(0) == '#')
+                       if (line.charAt(0) == '+')
                        {
                                field.isKey = true;
                                line = line.slice(1);
@@ -192,14 +192,16 @@ class ErDiags
                // Pass 2: parse associations, add foreign keys when cardinality is 0,1 or 1,1
                this.associations.forEach( a => {
                        let newTableAttrs = [ ];
+                       let hasZeroOne = false;
                        a.entities.forEach( e => {
                                if (['?','1'].includes(e.card[0]))
                                {
+                                       hasZeroOne = true;
                                        // Foreign key apparition (for each entity in association minus current one, for each identifying attribute)
                                        a.entities.forEach( e2 => {
                                                if (e2.name == e.name)
                                                        return;
-                                               e2.attributes.forEach( attr => {
+                                               this.entities[e2.name].attributes.forEach( attr => {
                                                        if (attr.isKey)
                                                        {
                                                                this.tables[e.name].push({
@@ -223,7 +225,7 @@ class ErDiags
                                        });
                                }
                        });
-                       if (newTableAttrs.length > 1)
+                       if (!hasZeroOne && newTableAttrs.length > 1)
                        {
                                // Ok, really create a new table
                                let newTable = {
@@ -242,7 +244,7 @@ class ErDiags
                                        });
                                });
                                // Add relationship potential own attributes
-                               a.attributes.forEach( attr => {
+                               (a.attributes || [ ]).forEach( attr => {
                                        newTable.fields.push({
                                                name: attr.name,
                                                isKey: false,