Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix: improve SQL export formatting and add schema-aware FK grouping (#783)

Jonathan Fishner6df588f40e6e
diff --git a/src/lib/data/export-metadata/export-per-type/mssql.ts b/src/lib/data/export-metadata/export-per-type/mssql.ts
index 7148180d..cd593a50 100644
--- a/src/lib/data/export-metadata/export-per-type/mssql.ts
+++ b/src/lib/data/export-metadata/export-per-type/mssql.ts
@@ -93,7 +93,7 @@ export function exportMSSQL(diagram: Diagram): string {
// Add schema creation statements
schemas.forEach((schema) => {
- sqlScript += `IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '${schema}')\nBEGIN\n EXEC('CREATE SCHEMA [${schema}]');\nEND;\n\n`;
+ sqlScript += `IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '${schema}')\nBEGIN\n EXEC('CREATE SCHEMA [${schema}]');\nEND;\n`;
});
// Generate table creation SQL
@@ -171,78 +171,160 @@ export function exportMSSQL(diagram: Diagram): string {
.map((f) => `[${f.name}]`)
.join(', ')})`
: ''
- }\n);\n\n${table.indexes
- .map((index) => {
- const indexName = table.schema
- ? `[${table.schema}_${index.name}]`
- : `[${index.name}]`;
- const indexFields = index.fieldIds
- .map((fieldId) => {
- const field = table.fields.find(
- (f) => f.id === fieldId
+ }\n);\n${(() => {
+ const validIndexes = table.indexes
+ .map((index) => {
+ const indexName = table.schema
+ ? `[${table.schema}_${index.name}]`
+ : `[${index.name}]`;
+ const indexFields = index.fieldIds
+ .map((fieldId) => {
+ const field = table.fields.find(
+ (f) => f.id === fieldId
+ );
+ return field ? `[${field.name}]` : '';
+ })
+ .filter(Boolean);
+
+ // SQL Server has a limit of 32 columns in an index
+ if (indexFields.length > 32) {
+ const warningComment = `/* WARNING: This index originally had ${indexFields.length} columns. It has been truncated to 32 columns due to SQL Server's index column limit. */\n`;
+ console.warn(
+ `Warning: Index ${indexName} on table ${tableName} has ${indexFields.length} columns. SQL Server limits indexes to 32 columns. The index will be truncated.`
);
- return field ? `[${field.name}]` : '';
- })
- .filter(Boolean);
-
- // SQL Server has a limit of 32 columns in an index
- if (indexFields.length > 32) {
- const warningComment = `/* WARNING: This index originally had ${indexFields.length} columns. It has been truncated to 32 columns due to SQL Server's index column limit. */\n`;
- console.warn(
- `Warning: Index ${indexName} on table ${tableName} has ${indexFields.length} columns. SQL Server limits indexes to 32 columns. The index will be truncated.`
- );
- indexFields.length = 32;
+ indexFields.length = 32;
+ return indexFields.length > 0
+ ? `${warningComment}CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFields.join(', ')});`
+ : '';
+ }
+
return indexFields.length > 0
- ? `${warningComment}CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFields.join(', ')});\n\n`
+ ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFields.join(', ')});`
: '';
- }
+ })
+ .filter(Boolean);
- return indexFields.length > 0
- ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFields.join(', ')});\n\n`
- : '';
- })
- .join('')}`;
+ return validIndexes.length > 0
+ ? `\n-- Indexes\n${validIndexes.join('\n')}`
+ : '';
+ })()}\n`;
})
.filter(Boolean) // Remove empty strings (views)
.join('\n');
// Generate foreign keys
- sqlScript += `\n${relationships
- .map((r: DBRelationship) => {
- const sourceTable = tables.find((t) => t.id === r.sourceTableId);
- const targetTable = tables.find((t) => t.id === r.targetTableId);
-
- if (
- !sourceTable ||
- !targetTable ||
- sourceTable.isView ||
- targetTable.isView
- ) {
- return '';
- }
+ if (relationships.length > 0) {
+ sqlScript += '\n-- Foreign key constraints\n';
- const sourceField = sourceTable.fields.find(
- (f) => f.id === r.sourceFieldId
- );
- const targetField = targetTable.fields.find(
- (f) => f.id === r.targetFieldId
- );
+ // Process all relationships and create FK objects with schema info
+ const foreignKeys = relationships
+ .map((r: DBRelationship) => {
+ const sourceTable = tables.find(
+ (t) => t.id === r.sourceTableId
+ );
+ const targetTable = tables.find(
+ (t) => t.id === r.targetTableId
+ );
- if (!sourceField || !targetField) {
- return '';
- }
+ if (
+ !sourceTable ||
+ !targetTable ||
+ sourceTable.isView ||
+ targetTable.isView
+ ) {
+ return '';
+ }
- const sourceTableName = sourceTable.schema
- ? `[${sourceTable.schema}].[${sourceTable.name}]`
- : `[${sourceTable.name}]`;
- const targetTableName = targetTable.schema
- ? `[${targetTable.schema}].[${targetTable.name}]`
- : `[${targetTable.name}]`;
+ const sourceField = sourceTable.fields.find(
+ (f) => f.id === r.sourceFieldId
+ );
+ const targetField = targetTable.fields.find(
+ (f) => f.id === r.targetFieldId
+ );
- return `ALTER TABLE ${sourceTableName}\nADD CONSTRAINT [${r.name}] FOREIGN KEY([${sourceField.name}]) REFERENCES ${targetTableName}([${targetField.name}]);\n`;
- })
- .filter(Boolean) // Remove empty strings
- .join('\n')}`;
+ if (!sourceField || !targetField) {
+ return '';
+ }
+
+ // Determine which table should have the foreign key based on cardinality
+ let fkTable, fkField, refTable, refField;
+
+ if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'many'
+ ) {
+ // FK goes on target table
+ fkTable = targetTable;
+ fkField = targetField;
+ refTable = sourceTable;
+ refField = sourceField;
+ } else if (
+ r.sourceCardinality === 'many' &&
+ r.targetCardinality === 'one'
+ ) {
+ // FK goes on source table
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'one'
+ ) {
+ // For 1:1, FK can go on either side, but typically goes on the table that references the other
+ // We'll keep the current behavior for 1:1
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else {
+ // Many-to-many relationships need a junction table, skip for now
+ return '';
+ }
+
+ const fkTableName = fkTable.schema
+ ? `[${fkTable.schema}].[${fkTable.name}]`
+ : `[${fkTable.name}]`;
+ const refTableName = refTable.schema
+ ? `[${refTable.schema}].[${refTable.name}]`
+ : `[${refTable.name}]`;
+
+ return {
+ schema: fkTable.schema || 'dbo',
+ sql: `ALTER TABLE ${fkTableName} ADD CONSTRAINT [${r.name}] FOREIGN KEY([${fkField.name}]) REFERENCES ${refTableName}([${refField.name}]);`,
+ };
+ })
+ .filter(Boolean); // Remove empty objects
+
+ // Group foreign keys by schema
+ const fksBySchema = foreignKeys.reduce(
+ (acc, fk) => {
+ if (!fk) return acc;
+ const schema = fk.schema;
+ if (!acc[schema]) {
+ acc[schema] = [];
+ }
+ acc[schema].push(fk.sql);
+ return acc;
+ },
+ {} as Record<string, string[]>
+ );
+
+ // Sort schemas and generate SQL with separators
+ const sortedSchemas = Object.keys(fksBySchema).sort();
+ const fkSql = sortedSchemas
+ .map((schema, index) => {
+ const schemaFks = fksBySchema[schema].join('\n');
+ if (index === 0) {
+ return `-- Schema: ${schema}\n${schemaFks}`;
+ } else {
+ return `\n-- Schema: ${schema}\n${schemaFks}`;
+ }
+ })
+ .join('\n');
+
+ sqlScript += fkSql;
+ }
return sqlScript;
}
diff --git a/src/lib/data/export-metadata/export-per-type/mysql.ts b/src/lib/data/export-metadata/export-per-type/mysql.ts
index a9c51ab3..acf99d63 100644
--- a/src/lib/data/export-metadata/export-per-type/mysql.ts
+++ b/src/lib/data/export-metadata/export-per-type/mysql.ts
@@ -179,10 +179,10 @@ export function exportMySQL(diagram: Diagram): string {
const relationships = diagram.relationships;
// Start SQL script
- let sqlScript = '-- MySQL database export\n\n';
+ let sqlScript = '-- MySQL database export\n';
// MySQL doesn't really use transactions for DDL statements but we'll add it for consistency
- sqlScript += 'START TRANSACTION;\n\n';
+ sqlScript += 'START TRANSACTION;\n';
// Create databases (schemas) if they don't exist
const schemas = new Set<string>();
@@ -218,7 +218,7 @@ export function exportMySQL(diagram: Diagram): string {
return `${
table.comments ? formatTableComment(table.comments) : ''
- }CREATE TABLE IF NOT EXISTS ${tableName} (\n${table.fields
+ }\nCREATE TABLE IF NOT EXISTS ${tableName} (\n${table.fields
.map((field: DBField) => {
const fieldName = `\`${field.name}\``;
@@ -308,95 +308,105 @@ export function exportMySQL(diagram: Diagram): string {
table.comments
? ` COMMENT='${escapeSQLComment(table.comments)}'`
: ''
- };\n\n${
+ };\n${
// Add indexes - MySQL creates them separately from the table definition
- table.indexes
- .map((index) => {
- // Get the list of fields for this index
- const indexFields = index.fieldIds
- .map((fieldId) => {
- const field = table.fields.find(
- (f) => f.id === fieldId
- );
- return field ? field : null;
- })
- .filter(Boolean);
-
- // Skip if this index exactly matches the primary key fields
- if (
- primaryKeyFields.length === indexFields.length &&
- primaryKeyFields.every((pk) =>
- indexFields.some(
- (field) => field && field.id === pk.id
+ (() => {
+ const validIndexes = table.indexes
+ .map((index) => {
+ // Get the list of fields for this index
+ const indexFields = index.fieldIds
+ .map((fieldId) => {
+ const field = table.fields.find(
+ (f) => f.id === fieldId
+ );
+ return field ? field : null;
+ })
+ .filter(Boolean);
+
+ // Skip if this index exactly matches the primary key fields
+ if (
+ primaryKeyFields.length ===
+ indexFields.length &&
+ primaryKeyFields.every((pk) =>
+ indexFields.some(
+ (field) => field && field.id === pk.id
+ )
)
- )
- ) {
- return '';
- }
-
- // Create a unique index name by combining table name, field names, and a unique/non-unique indicator
- const fieldNamesForIndex = indexFields
- .map((field) => field?.name || '')
- .join('_');
- const uniqueIndicator = index.unique ? '_unique' : '';
- const indexName = `\`idx_${table.name}_${fieldNamesForIndex}${uniqueIndicator}\``;
-
- // Get the properly quoted field names
- const indexFieldNames = indexFields
- .map((field) => (field ? `\`${field.name}\`` : ''))
- .filter(Boolean);
-
- // Check for text/blob fields that need special handling
- const hasTextOrBlob = indexFields.some((field) => {
- const typeName =
- field?.type.name.toLowerCase() || '';
- return (
- typeName === 'text' ||
- typeName === 'mediumtext' ||
- typeName === 'longtext' ||
- typeName === 'blob'
- );
- });
-
- // If there are TEXT/BLOB fields, need to add prefix length
- const indexFieldsWithPrefix = hasTextOrBlob
- ? indexFieldNames.map((name) => {
- const field = indexFields.find(
- (f) => `\`${f?.name}\`` === name
- );
- if (!field) return name;
-
- const typeName =
- field.type.name.toLowerCase();
- if (
- typeName === 'text' ||
- typeName === 'mediumtext' ||
- typeName === 'longtext' ||
- typeName === 'blob'
- ) {
- // Add a prefix length for TEXT/BLOB fields (required in MySQL)
- return `${name}(255)`;
- }
- return name;
- })
- : indexFieldNames;
-
- return indexFieldNames.length > 0
- ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFieldsWithPrefix.join(', ')});\n`
- : '';
- })
- .filter(Boolean)
- .join('\n')
- }`;
+ ) {
+ return '';
+ }
+
+ // Create a unique index name by combining table name, field names, and a unique/non-unique indicator
+ const fieldNamesForIndex = indexFields
+ .map((field) => field?.name || '')
+ .join('_');
+ const uniqueIndicator = index.unique
+ ? '_unique'
+ : '';
+ const indexName = `\`idx_${table.name}_${fieldNamesForIndex}${uniqueIndicator}\``;
+
+ // Get the properly quoted field names
+ const indexFieldNames = indexFields
+ .map((field) =>
+ field ? `\`${field.name}\`` : ''
+ )
+ .filter(Boolean);
+
+ // Check for text/blob fields that need special handling
+ const hasTextOrBlob = indexFields.some((field) => {
+ const typeName =
+ field?.type.name.toLowerCase() || '';
+ return (
+ typeName === 'text' ||
+ typeName === 'mediumtext' ||
+ typeName === 'longtext' ||
+ typeName === 'blob'
+ );
+ });
+
+ // If there are TEXT/BLOB fields, need to add prefix length
+ const indexFieldsWithPrefix = hasTextOrBlob
+ ? indexFieldNames.map((name) => {
+ const field = indexFields.find(
+ (f) => `\`${f?.name}\`` === name
+ );
+ if (!field) return name;
+
+ const typeName =
+ field.type.name.toLowerCase();
+ if (
+ typeName === 'text' ||
+ typeName === 'mediumtext' ||
+ typeName === 'longtext' ||
+ typeName === 'blob'
+ ) {
+ // Add a prefix length for TEXT/BLOB fields (required in MySQL)
+ return `${name}(255)`;
+ }
+ return name;
+ })
+ : indexFieldNames;
+
+ return indexFieldNames.length > 0
+ ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${indexFieldsWithPrefix.join(', ')});`
+ : '';
+ })
+ .filter(Boolean);
+
+ return validIndexes.length > 0
+ ? `\n-- Indexes\n${validIndexes.join('\n')}`
+ : '';
+ })()
+ }\n`;
})
.filter(Boolean) // Remove empty strings (views)
.join('\n');
// Generate foreign keys
if (relationships.length > 0) {
- sqlScript += '\n-- Foreign key constraints\n\n';
+ sqlScript += '\n-- Foreign key constraints\n';
- sqlScript += relationships
+ const foreignKeys = relationships
.map((r: DBRelationship) => {
const sourceTable = tables.find(
(t) => t.id === r.sourceTableId
@@ -425,25 +435,62 @@ export function exportMySQL(diagram: Diagram): string {
return '';
}
- const sourceTableName = sourceTable.schema
- ? `\`${sourceTable.schema}\`.\`${sourceTable.name}\``
- : `\`${sourceTable.name}\``;
- const targetTableName = targetTable.schema
- ? `\`${targetTable.schema}\`.\`${targetTable.name}\``
- : `\`${targetTable.name}\``;
+ // Determine which table should have the foreign key based on cardinality
+ let fkTable, fkField, refTable, refField;
+
+ if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'many'
+ ) {
+ // FK goes on target table
+ fkTable = targetTable;
+ fkField = targetField;
+ refTable = sourceTable;
+ refField = sourceField;
+ } else if (
+ r.sourceCardinality === 'many' &&
+ r.targetCardinality === 'one'
+ ) {
+ // FK goes on source table
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'one'
+ ) {
+ // For 1:1, FK can go on either side, but typically goes on the table that references the other
+ // We'll keep the current behavior for 1:1
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else {
+ // Many-to-many relationships need a junction table, skip for now
+ return '';
+ }
+
+ const fkTableName = fkTable.schema
+ ? `\`${fkTable.schema}\`.\`${fkTable.name}\``
+ : `\`${fkTable.name}\``;
+ const refTableName = refTable.schema
+ ? `\`${refTable.schema}\`.\`${refTable.name}\``
+ : `\`${refTable.name}\``;
// Create a descriptive constraint name
- const constraintName = `\`fk_${sourceTable.name}_${sourceField.name}\``;
+ const constraintName = `\`fk_${fkTable.name}_${fkField.name}\``;
// MySQL supports ON DELETE and ON UPDATE actions
- return `ALTER TABLE ${sourceTableName}\nADD CONSTRAINT ${constraintName} FOREIGN KEY(\`${sourceField.name}\`) REFERENCES ${targetTableName}(\`${targetField.name}\`)\nON UPDATE CASCADE ON DELETE RESTRICT;\n`;
+ return `ALTER TABLE ${fkTableName} ADD CONSTRAINT ${constraintName} FOREIGN KEY(\`${fkField.name}\`) REFERENCES ${refTableName}(\`${refField.name}\`);`;
})
- .filter(Boolean) // Remove empty strings
- .join('\n');
+ .filter(Boolean); // Remove empty strings
+
+ sqlScript += foreignKeys.join('\n');
}
// Commit transaction
- sqlScript += '\nCOMMIT;\n';
+ sqlScript += '\n\nCOMMIT;\n';
return sqlScript;
}
diff --git a/src/lib/data/export-metadata/export-per-type/postgresql.ts b/src/lib/data/export-metadata/export-per-type/postgresql.ts
index c98c5f02..a7084d66 100644
--- a/src/lib/data/export-metadata/export-per-type/postgresql.ts
+++ b/src/lib/data/export-metadata/export-per-type/postgresql.ts
@@ -142,7 +142,7 @@ function exportCustomTypes(customTypes: DBCustomType[]): string {
}
});
- return typesSql + '\n';
+ return typesSql ? typesSql + '\n' : '';
}
export function exportPostgreSQL(diagram: Diagram): string {
@@ -175,7 +175,9 @@ export function exportPostgreSQL(diagram: Diagram): string {
schemas.forEach((schema) => {
sqlScript += `CREATE SCHEMA IF NOT EXISTS "${schema}";\n`;
});
- sqlScript += '\n';
+ if (schemas.size > 0) {
+ sqlScript += '\n';
+ }
// Add custom types (enums and composite types)
sqlScript += exportCustomTypes(customTypes);
@@ -200,7 +202,9 @@ export function exportPostgreSQL(diagram: Diagram): string {
sequences.forEach((sequence) => {
sqlScript += `CREATE SEQUENCE IF NOT EXISTS ${sequence};\n`;
});
- sqlScript += '\n';
+ if (sequences.size > 0) {
+ sqlScript += '\n';
+ }
// Generate table creation SQL
sqlScript += tables
@@ -310,10 +314,10 @@ export function exportPostgreSQL(diagram: Diagram): string {
.map((f) => `"${f.name}"`)
.join(', ')})`
: ''
- }\n);\n\n${
+ }\n);${
// Add table comments
table.comments
- ? `COMMENT ON TABLE ${tableName} IS '${escapeSQLComment(table.comments)}';\n\n`
+ ? `\nCOMMENT ON TABLE ${tableName} IS '${escapeSQLComment(table.comments)}';`
: ''
}${
// Add column comments
@@ -321,125 +325,211 @@ export function exportPostgreSQL(diagram: Diagram): string {
.filter((f) => f.comments)
.map(
(f) =>
- `COMMENT ON COLUMN ${tableName}."${f.name}" IS '${escapeSQLComment(f.comments || '')}';\n`
+ `\nCOMMENT ON COLUMN ${tableName}."${f.name}" IS '${escapeSQLComment(f.comments || '')}';`
)
.join('')
- }\n${
+ }${
// Add indexes only for non-primary key fields or composite indexes
// This avoids duplicate indexes on primary key columns
- table.indexes
- .map((index) => {
- // Get the list of fields for this index
- const indexFields = index.fieldIds
- .map((fieldId) => {
- const field = table.fields.find(
- (f) => f.id === fieldId
- );
- return field ? field : null;
- })
- .filter(Boolean);
-
- // Skip if this index exactly matches the primary key fields
- // This prevents creating redundant indexes
- if (
- primaryKeyFields.length === indexFields.length &&
- primaryKeyFields.every((pk) =>
- indexFields.some(
- (field) => field && field.id === pk.id
+ (() => {
+ const validIndexes = table.indexes
+ .map((index) => {
+ // Get the list of fields for this index
+ const indexFields = index.fieldIds
+ .map((fieldId) => {
+ const field = table.fields.find(
+ (f) => f.id === fieldId
+ );
+ return field ? field : null;
+ })
+ .filter(Boolean);
+
+ // Skip if this index exactly matches the primary key fields
+ // This prevents creating redundant indexes
+ if (
+ primaryKeyFields.length ===
+ indexFields.length &&
+ primaryKeyFields.every((pk) =>
+ indexFields.some(
+ (field) => field && field.id === pk.id
+ )
)
- )
- ) {
- return '';
- }
-
- // Create unique index name using table name and index name
- // This ensures index names are unique across the database
- const safeTableName = table.name.replace(
- /[^a-zA-Z0-9_]/g,
- '_'
- );
- const safeIndexName = index.name.replace(
- /[^a-zA-Z0-9_]/g,
- '_'
- );
-
- // Limit index name length to avoid PostgreSQL's 63-character identifier limit
- let combinedName = `${safeTableName}_${safeIndexName}`;
- if (combinedName.length > 60) {
- // If too long, use just the index name or a truncated version
- combinedName =
- safeIndexName.length > 60
- ? safeIndexName.substring(0, 60)
- : safeIndexName;
- }
-
- const indexName = `"${combinedName}"`;
-
- // Get the properly quoted field names
- const indexFieldNames = indexFields
- .map((field) => (field ? `"${field.name}"` : ''))
- .filter(Boolean);
-
- return indexFieldNames.length > 0
- ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFieldNames.join(', ')});\n\n`
- : '';
- })
- .filter(Boolean)
- .join('')
- }`;
+ ) {
+ return '';
+ }
+
+ // Create unique index name using table name and index name
+ // This ensures index names are unique across the database
+ const safeTableName = table.name.replace(
+ /[^a-zA-Z0-9_]/g,
+ '_'
+ );
+ const safeIndexName = index.name.replace(
+ /[^a-zA-Z0-9_]/g,
+ '_'
+ );
+
+ // Limit index name length to avoid PostgreSQL's 63-character identifier limit
+ let combinedName = `${safeTableName}_${safeIndexName}`;
+ if (combinedName.length > 60) {
+ // If too long, use just the index name or a truncated version
+ combinedName =
+ safeIndexName.length > 60
+ ? safeIndexName.substring(0, 60)
+ : safeIndexName;
+ }
+
+ const indexName = `"${combinedName}"`;
+
+ // Get the properly quoted field names
+ const indexFieldNames = indexFields
+ .map((field) =>
+ field ? `"${field.name}"` : ''
+ )
+ .filter(Boolean);
+
+ return indexFieldNames.length > 0
+ ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${indexFieldNames.join(', ')});`
+ : '';
+ })
+ .filter(Boolean);
+
+ return validIndexes.length > 0
+ ? `\n-- Indexes\n${validIndexes.join('\n')}`
+ : '';
+ })()
+ }\n`;
})
.filter(Boolean) // Remove empty strings (views)
.join('\n');
// Generate foreign keys
- sqlScript += `\n${relationships
- .map((r: DBRelationship) => {
- const sourceTable = tables.find((t) => t.id === r.sourceTableId);
- const targetTable = tables.find((t) => t.id === r.targetTableId);
-
- if (
- !sourceTable ||
- !targetTable ||
- sourceTable.isView ||
- targetTable.isView
- ) {
- return '';
- }
+ if (relationships.length > 0) {
+ sqlScript += '\n-- Foreign key constraints\n';
+
+ // Process all relationships and create FK objects with schema info
+ const foreignKeys = relationships
+ .map((r: DBRelationship) => {
+ const sourceTable = tables.find(
+ (t) => t.id === r.sourceTableId
+ );
+ const targetTable = tables.find(
+ (t) => t.id === r.targetTableId
+ );
- const sourceField = sourceTable.fields.find(
- (f) => f.id === r.sourceFieldId
- );
- const targetField = targetTable.fields.find(
- (f) => f.id === r.targetFieldId
- );
+ if (
+ !sourceTable ||
+ !targetTable ||
+ sourceTable.isView ||
+ targetTable.isView
+ ) {
+ return '';
+ }
- if (!sourceField || !targetField) {
- return '';
- }
+ const sourceField = sourceTable.fields.find(
+ (f) => f.id === r.sourceFieldId
+ );
+ const targetField = targetTable.fields.find(
+ (f) => f.id === r.targetFieldId
+ );
- const sourceTableName = sourceTable.schema
- ? `"${sourceTable.schema}"."${sourceTable.name}"`
- : `"${sourceTable.name}"`;
- const targetTableName = targetTable.schema
- ? `"${targetTable.schema}"."${targetTable.name}"`
- : `"${targetTable.name}"`;
-
- // Create a unique constraint name by combining table and field names
- // Ensure it stays within PostgreSQL's 63-character limit for identifiers
- // and doesn't get truncated in a way that breaks SQL syntax
- const baseName = `fk_${sourceTable.name}_${sourceField.name}_${targetTable.name}_${targetField.name}`;
- // Limit to 60 chars (63 minus quotes) to ensure the whole identifier stays within limits
- const safeConstraintName =
- baseName.length > 60
- ? baseName.substring(0, 60).replace(/[^a-zA-Z0-9_]/g, '_')
- : baseName.replace(/[^a-zA-Z0-9_]/g, '_');
-
- const constraintName = `"${safeConstraintName}"`;
-
- return `ALTER TABLE ${sourceTableName}\nADD CONSTRAINT ${constraintName} FOREIGN KEY("${sourceField.name}") REFERENCES ${targetTableName}("${targetField.name}");\n`;
- })
- .filter(Boolean) // Remove empty strings
- .join('\n')}`;
+ if (!sourceField || !targetField) {
+ return '';
+ }
+
+ // Determine which table should have the foreign key based on cardinality
+ let fkTable, fkField, refTable, refField;
+
+ if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'many'
+ ) {
+ // FK goes on target table
+ fkTable = targetTable;
+ fkField = targetField;
+ refTable = sourceTable;
+ refField = sourceField;
+ } else if (
+ r.sourceCardinality === 'many' &&
+ r.targetCardinality === 'one'
+ ) {
+ // FK goes on source table
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'one'
+ ) {
+ // For 1:1, FK can go on either side, but typically goes on the table that references the other
+ // We'll keep the current behavior for 1:1
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else {
+ // Many-to-many relationships need a junction table, skip for now
+ return '';
+ }
+
+ const fkTableName = fkTable.schema
+ ? `"${fkTable.schema}"."${fkTable.name}"`
+ : `"${fkTable.name}"`;
+ const refTableName = refTable.schema
+ ? `"${refTable.schema}"."${refTable.name}"`
+ : `"${refTable.name}"`;
+
+ // Create a unique constraint name by combining table and field names
+ // Ensure it stays within PostgreSQL's 63-character limit for identifiers
+ // and doesn't get truncated in a way that breaks SQL syntax
+ const baseName = `fk_${fkTable.name}_${fkField.name}_${refTable.name}_${refField.name}`;
+ // Limit to 60 chars (63 minus quotes) to ensure the whole identifier stays within limits
+ const safeConstraintName =
+ baseName.length > 60
+ ? baseName
+ .substring(0, 60)
+ .replace(/[^a-zA-Z0-9_]/g, '_')
+ : baseName.replace(/[^a-zA-Z0-9_]/g, '_');
+
+ const constraintName = `"${safeConstraintName}"`;
+
+ return {
+ schema: fkTable.schema || 'public',
+ sql: `ALTER TABLE ${fkTableName} ADD CONSTRAINT ${constraintName} FOREIGN KEY("${fkField.name}") REFERENCES ${refTableName}("${refField.name}");`,
+ };
+ })
+ .filter(Boolean); // Remove empty objects
+
+ // Group foreign keys by schema
+ const fksBySchema = foreignKeys.reduce(
+ (acc, fk) => {
+ if (!fk) return acc;
+ const schema = fk.schema;
+ if (!acc[schema]) {
+ acc[schema] = [];
+ }
+ acc[schema].push(fk.sql);
+ return acc;
+ },
+ {} as Record<string, string[]>
+ );
+
+ // Sort schemas and generate SQL with separators
+ const sortedSchemas = Object.keys(fksBySchema).sort();
+ const fkSql = sortedSchemas
+ .map((schema, index) => {
+ const schemaFks = fksBySchema[schema].join('\n');
+ if (index === 0) {
+ return `-- Schema: ${schema}\n${schemaFks}`;
+ } else {
+ return `\n-- Schema: ${schema}\n${schemaFks}`;
+ }
+ })
+ .join('\n');
+
+ sqlScript += fkSql;
+ }
return sqlScript;
}
diff --git a/src/lib/data/export-metadata/export-per-type/sqlite.ts b/src/lib/data/export-metadata/export-per-type/sqlite.ts
index e897cac6..8a67befd 100644
--- a/src/lib/data/export-metadata/export-per-type/sqlite.ts
+++ b/src/lib/data/export-metadata/export-per-type/sqlite.ts
@@ -149,10 +149,10 @@ export function exportSQLite(diagram: Diagram): string {
const relationships = diagram.relationships;
// Start SQL script - SQLite doesn't use schemas, so we skip schema creation
- let sqlScript = '-- SQLite database export\n\n';
+ let sqlScript = '-- SQLite database export\n';
// Begin transaction for faster import
- sqlScript += 'BEGIN TRANSACTION;\n\n';
+ sqlScript += 'BEGIN TRANSACTION;\n';
// SQLite doesn't have sequences, so we skip sequence creation
@@ -264,49 +264,57 @@ export function exportSQLite(diagram: Diagram): string {
.map((f) => `"${f.name}"`)
.join(', ')})`
: ''
- }\n);\n\n${
+ }\n);\n${
// Add indexes - SQLite doesn't support indexes in CREATE TABLE
- table.indexes
- .map((index) => {
- // Skip indexes that exactly match the primary key
- const indexFields = index.fieldIds
- .map((fieldId) => {
- const field = table.fields.find(
- (f) => f.id === fieldId
- );
- return field ? field : null;
- })
- .filter(Boolean);
-
- // Get the properly quoted field names
- const indexFieldNames = indexFields
- .map((field) => (field ? `"${field.name}"` : ''))
- .filter(Boolean);
-
- // Skip if this index exactly matches the primary key fields
- if (
- primaryKeyFields.length === indexFields.length &&
- primaryKeyFields.every((pk) =>
- indexFields.some(
- (field) => field && field.id === pk.id
+ (() => {
+ const validIndexes = table.indexes
+ .map((index) => {
+ // Skip indexes that exactly match the primary key
+ const indexFields = index.fieldIds
+ .map((fieldId) => {
+ const field = table.fields.find(
+ (f) => f.id === fieldId
+ );
+ return field ? field : null;
+ })
+ .filter(Boolean);
+
+ // Get the properly quoted field names
+ const indexFieldNames = indexFields
+ .map((field) =>
+ field ? `"${field.name}"` : ''
)
- )
- ) {
- return '';
- }
-
- // Create safe index name
- const safeIndexName = `${table.name}_${index.name}`
- .replace(/[^a-zA-Z0-9_]/g, '_')
- .substring(0, 60);
-
- return indexFieldNames.length > 0
- ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX IF NOT EXISTS "${safeIndexName}"\nON ${tableName} (${indexFieldNames.join(', ')});\n`
- : '';
- })
- .filter(Boolean)
- .join('\n')
- }`;
+ .filter(Boolean);
+
+ // Skip if this index exactly matches the primary key fields
+ if (
+ primaryKeyFields.length ===
+ indexFields.length &&
+ primaryKeyFields.every((pk) =>
+ indexFields.some(
+ (field) => field && field.id === pk.id
+ )
+ )
+ ) {
+ return '';
+ }
+
+ // Create safe index name
+ const safeIndexName = `${table.name}_${index.name}`
+ .replace(/[^a-zA-Z0-9_]/g, '_')
+ .substring(0, 60);
+
+ return indexFieldNames.length > 0
+ ? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX IF NOT EXISTS "${safeIndexName}"\nON ${tableName} (${indexFieldNames.join(', ')});`
+ : '';
+ })
+ .filter(Boolean);
+
+ return validIndexes.length > 0
+ ? `\n-- Indexes\n${validIndexes.join('\n')}`
+ : '';
+ })()
+ }\n`;
})
.filter(Boolean) // Remove empty strings (views)
.join('\n');
@@ -319,7 +327,7 @@ export function exportSQLite(diagram: Diagram): string {
sqlScript += '\n-- Foreign key constraints\n';
sqlScript +=
'-- Note: SQLite requires foreign_keys pragma to be enabled:\n';
- sqlScript += '-- PRAGMA foreign_keys = ON;\n\n';
+ sqlScript += '-- PRAGMA foreign_keys = ON;\n';
relationships.forEach((r: DBRelationship) => {
const sourceTable = tables.find((t) => t.id === r.sourceTableId);
@@ -347,8 +355,44 @@ export function exportSQLite(diagram: Diagram): string {
return;
}
+ // Determine which table should have the foreign key based on cardinality
+ let fkTable, fkField, refTable, refField;
+
+ if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'many'
+ ) {
+ // FK goes on target table
+ fkTable = targetTable;
+ fkField = targetField;
+ refTable = sourceTable;
+ refField = sourceField;
+ } else if (
+ r.sourceCardinality === 'many' &&
+ r.targetCardinality === 'one'
+ ) {
+ // FK goes on source table
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else if (
+ r.sourceCardinality === 'one' &&
+ r.targetCardinality === 'one'
+ ) {
+ // For 1:1, FK can go on either side, but typically goes on the table that references the other
+ // We'll keep the current behavior for 1:1
+ fkTable = sourceTable;
+ fkField = sourceField;
+ refTable = targetTable;
+ refField = targetField;
+ } else {
+ // Many-to-many relationships need a junction table, skip for now
+ return;
+ }
+
// Create commented out version of what would be ALTER TABLE statement
- sqlScript += `-- ALTER TABLE "${sourceTable.name}" ADD CONSTRAINT "fk_${sourceTable.name}_${sourceField.name}" FOREIGN KEY("${sourceField.name}") REFERENCES "${targetTable.name}"("${targetField.name}");\n`;
+ sqlScript += `-- ALTER TABLE "${fkTable.name}" ADD CONSTRAINT "fk_${fkTable.name}_${fkField.name}" FOREIGN KEY("${fkField.name}") REFERENCES "${refTable.name}"("${refField.name}");\n`;
});
}
diff --git a/src/lib/data/export-metadata/export-sql-script.ts b/src/lib/data/export-metadata/export-sql-script.ts
index e8db78dc..2d5f5434 100644
--- a/src/lib/data/export-metadata/export-sql-script.ts
+++ b/src/lib/data/export-metadata/export-sql-script.ts
@@ -131,7 +131,23 @@ export const exportBaseSQL = ({
}
}
});
- sqlScript += '\n'; // Add a newline if custom types were processed
+ if (
+ diagram.customTypes.some(
+ (ct) =>
+ (ct.kind === 'enum' &&
+ ct.values &&
+ ct.values.length > 0 &&
+ targetDatabaseType === DatabaseType.POSTGRESQL &&
+ !isDBMLFlow) ||
+ (ct.kind === 'composite' &&
+ ct.fields &&
+ ct.fields.length > 0 &&
+ (targetDatabaseType === DatabaseType.POSTGRESQL ||
+ isDBMLFlow))
+ )
+ ) {
+ sqlScript += '\n';
+ }
}
// Add CREATE SEQUENCE statements
@@ -154,7 +170,9 @@ export const exportBaseSQL = ({
sequences.forEach((sequence) => {
sqlScript += `CREATE SEQUENCE IF NOT EXISTS ${sequence};\n`;
});
- sqlScript += '\n';
+ if (sequences.size > 0) {
+ sqlScript += '\n';
+ }
// Loop through each non-view table to generate the SQL statements
nonViewTables.forEach((table) => {
@@ -316,7 +334,7 @@ export const exportBaseSQL = ({
sqlScript += `\n PRIMARY KEY (${pkFieldNames})`;
}
- sqlScript += '\n);\n\n';
+ sqlScript += '\n);\n';
// Add table comment
if (table.comments) {
@@ -347,9 +365,11 @@ export const exportBaseSQL = ({
sqlScript += `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${fieldNames});\n`;
}
});
+ });
+ if (nonViewTables.length > 0 && (relationships?.length ?? 0) > 0) {
sqlScript += '\n';
- });
+ }
// Handle relationships (foreign keys)
relationships?.forEach((relationship) => {
@@ -373,13 +393,52 @@ export const exportBaseSQL = ({
sourceTableField &&
targetTableField
) {
- const sourceTableName = sourceTable.schema
- ? `${sourceTable.schema}.${sourceTable.name}`
- : sourceTable.name;
- const targetTableName = targetTable.schema
- ? `${targetTable.schema}.${targetTable.name}`
- : targetTable.name;
- sqlScript += `ALTER TABLE ${sourceTableName} ADD CONSTRAINT ${relationship.name} FOREIGN KEY (${sourceTableField.name}) REFERENCES ${targetTableName} (${targetTableField.name});\n`;
+ // Determine which table should have the foreign key based on cardinality
+ // In a 1:many relationship, the foreign key goes on the "many" side
+ // If source is "one" and target is "many", FK goes on target table
+ // If source is "many" and target is "one", FK goes on source table
+ let fkTable, fkField, refTable, refField;
+
+ if (
+ relationship.sourceCardinality === 'one' &&
+ relationship.targetCardinality === 'many'
+ ) {
+ // FK goes on target table
+ fkTable = targetTable;
+ fkField = targetTableField;
+ refTable = sourceTable;
+ refField = sourceTableField;
+ } else if (
+ relationship.sourceCardinality === 'many' &&
+ relationship.targetCardinality === 'one'
+ ) {
+ // FK goes on source table
+ fkTable = sourceTable;
+ fkField = sourceTableField;
+ refTable = targetTable;
+ refField = targetTableField;
+ } else if (
+ relationship.sourceCardinality === 'one' &&
+ relationship.targetCardinality === 'one'
+ ) {
+ // For 1:1, FK can go on either side, but typically goes on the table that references the other
+ // We'll keep the current behavior for 1:1
+ fkTable = sourceTable;
+ fkField = sourceTableField;
+ refTable = targetTable;
+ refField = targetTableField;
+ } else {
+ // Many-to-many relationships need a junction table, skip for now
+ return;
+ }
+
+ const fkTableName = fkTable.schema
+ ? `${fkTable.schema}.${fkTable.name}`
+ : fkTable.name;
+ const refTableName = refTable.schema
+ ? `${refTable.schema}.${refTable.name}`
+ : refTable.name;
+ sqlScript += `ALTER TABLE ${fkTableName} ADD CONSTRAINT ${relationship.name} FOREIGN KEY (${fkField.name}) REFERENCES ${refTableName} (${refField.name});\n`;
}
});