Skip to content

Fix foreign key generation#10

Merged
gumpdev merged 2 commits intogumpdev:masterfrom
FelipeRos19:master
Jan 22, 2026
Merged

Fix foreign key generation#10
gumpdev merged 2 commits intogumpdev:masterfrom
FelipeRos19:master

Conversation

@FelipeRos19
Copy link
Copy Markdown
Contributor

If an entity was used as a field (to generate a relationship) and the primary key of this entity contained the AUTO_INCREMENT attribute, it was loaded along with the query, generating incorrect SQL and preventing the table from being created.

To fix this problem, it was only necessary to clear the creation of the foreign key.

Old code: code

if(type == FieldType.ENTITY && fieldClass_relational != null){
            EntityMeta cMeta = Worm.getRegistry().getTableMeta(fieldClass_relational);
            if(cMeta.getUniqueKeyColumns().size() != 1)
                throw new WormException("Relational Worm tables need to have only one unique keys");
            WormField key = cMeta.getUniqueKeyColumns().toArray(WormField[]::new)[0];
            StringBuilder sql = new StringBuilder(key.getSqlCreation());
            sql.append(", CONSTRAINT FK_").append(getName()).append("_").append(cMeta.getName()).append(" FOREIGN KEY (").append(getName()).append(") REFERENCES ")
                    .append(cMeta.getName()).append(" (").append(key.getName()).append(")");
            if(onDelete != null)
                sql.append(" ON DELETE ").append(onDelete.getValue());
            if(onUpdate != null)
                sql.append(" ON UPDATE ").append(onUpdate.getValue());
            return sql.toString();
        }

New code:

if(type == FieldType.ENTITY && fieldClass_relational != null){
            EntityMeta cMeta = Worm.getRegistry().getTableMeta(fieldClass_relational);
            if (cMeta.getUniqueKeyColumns().size() != 1)
                throw new WormException("Relational Worm tables need to have only one unique keys");

            WormField key = cMeta.getUniqueKeyColumns().toArray(WormField[]::new)[0];

            StringBuilder sql = new StringBuilder();
            sql.append(key.getType().getValue());
            if (key.getLength() > 0)
                sql.append('(').append(key.getLength()).append(')');

            if (!nullable)
                sql.append(" NOT NULL");

            sql.append(", CONSTRAINT FK_").append(getName()).append("_").append(cMeta.getName())
                    .append(" FOREIGN KEY (").append(getName()).append(") REFERENCES ")
                    .append(cMeta.getName()).append(" (").append(key.getName()).append(")");

            if (onDelete != null)
                sql.append(" ON DELETE ").append(onDelete.name());

            if (onUpdate != null)
                sql.append(" ON UPDATE ").append(onUpdate.name());

            return sql.toString();
        }

The main difference between the old and new code is that the StringBuilder does not inherit the original characteristics; it only reconstructs them with the necessary data.

@gumpdev gumpdev merged commit 8dccd63 into gumpdev:master Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants