Fix bugs, improve schema gen
This commit is contained in:
@@ -3,7 +3,6 @@ import type {
|
||||
BunMariaDBConfig,
|
||||
} from "../../types";
|
||||
import buildColumnDefinition from "./build-column-definition";
|
||||
import buildForeignKeyConstraint from "./build-foreign-key-constraint";
|
||||
import buildTableOptions from "./build-table-options";
|
||||
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
||||
import runSchemaQuery from "./run-schema-query";
|
||||
@@ -20,7 +19,6 @@ export default async function createTable({
|
||||
}
|
||||
|
||||
const columnDefinitions: string[] = [];
|
||||
const foreignKeys: string[] = [];
|
||||
const primaryKeys: string[] = [];
|
||||
|
||||
for (const field of table.fields || []) {
|
||||
@@ -29,12 +27,6 @@ export default async function createTable({
|
||||
if (field.primaryKey && field.fieldName) {
|
||||
primaryKeys.push(field.fieldName);
|
||||
}
|
||||
|
||||
if (field.foreignKey) {
|
||||
foreignKeys.push(
|
||||
buildForeignKeyConstraint(field, table.tableName),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (primaryKeys.length > 0) {
|
||||
@@ -62,7 +54,12 @@ export default async function createTable({
|
||||
}
|
||||
}
|
||||
|
||||
const sql = `CREATE TABLE IF NOT EXISTS ${MariaDBQuoteGen(table.tableName)} (${[...columnDefinitions, ...foreignKeys].join(", ")})${buildTableOptions(table)}`;
|
||||
/**
|
||||
* Foreign keys are not declared inline. `handleDBSchemaTables` adds them
|
||||
* with `ensureForeignKeys` once every table exists, so forward references,
|
||||
* self references and FK cycles all work regardless of table order.
|
||||
*/
|
||||
const sql = `CREATE TABLE IF NOT EXISTS ${MariaDBQuoteGen(table.tableName)} (${columnDefinitions.join(", ")})${buildTableOptions(table)}`;
|
||||
|
||||
await runSchemaQuery({ query: sql, config });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user