diff --git a/packages/server/scripts/integrations/mysql/init.sql b/packages/server/scripts/integrations/mysql/init.sql index 15269f2f41..ae5cd07788 100644 --- a/packages/server/scripts/integrations/mysql/init.sql +++ b/packages/server/scripts/integrations/mysql/init.sql @@ -3,7 +3,7 @@ USE main; CREATE TABLE Persons ( PersonID int NOT NULL AUTO_INCREMENT, CreatedAt datetime, - Age float, + Age float DEFAULT 20 NOT NULL, LastName varchar(255), FirstName varchar(255), Address varchar(255), diff --git a/packages/server/scripts/integrations/postgres/init.sql b/packages/server/scripts/integrations/postgres/init.sql index 78af4c20b9..057944101a 100644 --- a/packages/server/scripts/integrations/postgres/init.sql +++ b/packages/server/scripts/integrations/postgres/init.sql @@ -8,6 +8,7 @@ CREATE TABLE Persons ( FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Belfast', + Age INTEGER DEFAULT 20 NOT NULL, Type person_job ); CREATE TABLE Tasks ( diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 2da190df5e..87da1ae81e 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -243,11 +243,13 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { if (typeof name !== "string") { continue } + const isAuto = !!autoColumns.find(col => col === name) + const required = !!requiredColumns.find(col => col === name) schema[name] = { - autocolumn: !!autoColumns.find(col => col === name), + autocolumn: isAuto, name: name, constraints: { - presence: requiredColumns.find(col => col === name), + presence: required && !isAuto, }, ...convertSqlType(def.DATA_TYPE), externalType: def.DATA_TYPE, diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 080939593f..8d984ed402 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -229,13 +229,15 @@ class MySQLIntegration extends Sql implements DatasourcePlus { if (column.Key === "PRI" && primaryKeys.indexOf(column.Key) === -1) { primaryKeys.push(columnName) } - const constraints = { - presence: column.Null !== "YES", - } + const hasDefault = column.Default != null const isAuto: boolean = typeof column.Extra === "string" && (column.Extra === "auto_increment" || column.Extra.toLowerCase().includes("generated")) + const required = column.Null !== "YES" + const constraints = { + presence: required && !isAuto && !hasDefault, + } schema[columnName] = { name: columnName, autocolumn: isAuto, diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index eba0d3691d..c981c3acc5 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -262,15 +262,17 @@ class PostgresIntegration extends Sql implements DatasourcePlus { column.identity_start || column.identity_increment ) - const constraints = { - presence: column.is_nullable === "NO", - } - const hasDefault = + const hasDefault = column.column_default != null + const hasNextVal = typeof column.column_default === "string" && column.column_default.startsWith("nextval") const isGenerated = column.is_generated && column.is_generated !== "NEVER" - const isAuto: boolean = hasDefault || identity || isGenerated + const isAuto: boolean = hasNextVal || identity || isGenerated + const required = column.is_nullable === "NO" + const constraints = { + presence: required && !hasDefault && !isGenerated, + } tables[tableName].schema[columnName] = { autocolumn: isAuto, name: columnName,