Merge pull request #10312 from Budibase/fix/10221
Fix for NOT NULL columns with defaults in SQL
This commit is contained in:
commit
0aa8f4f6c9
|
@ -11,6 +11,7 @@ if [ "$1" = '/opt/mssql/bin/sqlservr' ]; then
|
||||||
|
|
||||||
echo "RUNNING BUDIBASE SETUP"
|
echo "RUNNING BUDIBASE SETUP"
|
||||||
|
|
||||||
|
cat setup.sql
|
||||||
#run the setup script to create the DB and the schema in the DB
|
#run the setup script to create the DB and the schema in the DB
|
||||||
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Passw0rd -i setup.sql
|
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Passw0rd -i setup.sql
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ GO
|
||||||
CREATE TABLE people
|
CREATE TABLE people
|
||||||
(
|
(
|
||||||
name varchar(30) NOT NULL,
|
name varchar(30) NOT NULL,
|
||||||
age varchar(20),
|
age int default 20 NOT NULL,
|
||||||
CONSTRAINT pk_people PRIMARY KEY NONCLUSTERED (name, age)
|
CONSTRAINT pk_people PRIMARY KEY NONCLUSTERED (name, age)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -50,22 +50,22 @@ VALUES
|
||||||
('Processing', 1);
|
('Processing', 1);
|
||||||
|
|
||||||
INSERT INTO people (name, age)
|
INSERT INTO people (name, age)
|
||||||
VALUES ('Bob', '30'),
|
VALUES ('Bob', 30),
|
||||||
('Bert', '10'),
|
('Bert', 10),
|
||||||
('Jack', '12'),
|
('Jack', 12),
|
||||||
('Mike', '31'),
|
('Mike', 31),
|
||||||
('Dave', '44'),
|
('Dave', 44),
|
||||||
('Jim', '43'),
|
('Jim', 43),
|
||||||
('Kerry', '32'),
|
('Kerry', 32),
|
||||||
('Julie', '12'),
|
('Julie', 12),
|
||||||
('Kim', '55'),
|
('Kim', 55),
|
||||||
('Andy', '33'),
|
('Andy', 33),
|
||||||
('John', '22'),
|
('John', 22),
|
||||||
('Ruth', '66'),
|
('Ruth', 66),
|
||||||
('Robert', '88'),
|
('Robert', 88),
|
||||||
('Bobert', '99'),
|
('Bobert', 99),
|
||||||
('Jan', '22'),
|
('Jan', 22),
|
||||||
('Megan', '11');
|
('Megan', 11);
|
||||||
|
|
||||||
|
|
||||||
IF OBJECT_ID ('Chains.sizes', 'U') IS NOT NULL
|
IF OBJECT_ID ('Chains.sizes', 'U') IS NOT NULL
|
||||||
|
|
|
@ -3,7 +3,7 @@ USE main;
|
||||||
CREATE TABLE Persons (
|
CREATE TABLE Persons (
|
||||||
PersonID int NOT NULL AUTO_INCREMENT,
|
PersonID int NOT NULL AUTO_INCREMENT,
|
||||||
CreatedAt datetime,
|
CreatedAt datetime,
|
||||||
Age float,
|
Age float DEFAULT 20 NOT NULL,
|
||||||
LastName varchar(255),
|
LastName varchar(255),
|
||||||
FirstName varchar(255),
|
FirstName varchar(255),
|
||||||
Address varchar(255),
|
Address varchar(255),
|
||||||
|
|
|
@ -8,6 +8,7 @@ CREATE TABLE Persons (
|
||||||
FirstName varchar(255),
|
FirstName varchar(255),
|
||||||
Address varchar(255),
|
Address varchar(255),
|
||||||
City varchar(255) DEFAULT 'Belfast',
|
City varchar(255) DEFAULT 'Belfast',
|
||||||
|
Age INTEGER DEFAULT 20 NOT NULL,
|
||||||
Type person_job
|
Type person_job
|
||||||
);
|
);
|
||||||
CREATE TABLE Tasks (
|
CREATE TABLE Tasks (
|
||||||
|
|
|
@ -243,11 +243,14 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
|
||||||
if (typeof name !== "string") {
|
if (typeof name !== "string") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
const hasDefault = def.COLUMN_DEFAULT
|
||||||
|
const isAuto = !!autoColumns.find(col => col === name)
|
||||||
|
const required = !!requiredColumns.find(col => col === name)
|
||||||
schema[name] = {
|
schema[name] = {
|
||||||
autocolumn: !!autoColumns.find(col => col === name),
|
autocolumn: isAuto,
|
||||||
name: name,
|
name: name,
|
||||||
constraints: {
|
constraints: {
|
||||||
presence: requiredColumns.find(col => col === name),
|
presence: required && !isAuto && !hasDefault,
|
||||||
},
|
},
|
||||||
...convertSqlType(def.DATA_TYPE),
|
...convertSqlType(def.DATA_TYPE),
|
||||||
externalType: def.DATA_TYPE,
|
externalType: def.DATA_TYPE,
|
||||||
|
|
|
@ -229,13 +229,15 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
|
||||||
if (column.Key === "PRI" && primaryKeys.indexOf(column.Key) === -1) {
|
if (column.Key === "PRI" && primaryKeys.indexOf(column.Key) === -1) {
|
||||||
primaryKeys.push(columnName)
|
primaryKeys.push(columnName)
|
||||||
}
|
}
|
||||||
const constraints = {
|
const hasDefault = column.Default != null
|
||||||
presence: column.Null !== "YES",
|
|
||||||
}
|
|
||||||
const isAuto: boolean =
|
const isAuto: boolean =
|
||||||
typeof column.Extra === "string" &&
|
typeof column.Extra === "string" &&
|
||||||
(column.Extra === "auto_increment" ||
|
(column.Extra === "auto_increment" ||
|
||||||
column.Extra.toLowerCase().includes("generated"))
|
column.Extra.toLowerCase().includes("generated"))
|
||||||
|
const required = column.Null !== "YES"
|
||||||
|
const constraints = {
|
||||||
|
presence: required && !isAuto && !hasDefault,
|
||||||
|
}
|
||||||
schema[columnName] = {
|
schema[columnName] = {
|
||||||
name: columnName,
|
name: columnName,
|
||||||
autocolumn: isAuto,
|
autocolumn: isAuto,
|
||||||
|
|
|
@ -262,15 +262,17 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
|
||||||
column.identity_start ||
|
column.identity_start ||
|
||||||
column.identity_increment
|
column.identity_increment
|
||||||
)
|
)
|
||||||
const constraints = {
|
const hasDefault = column.column_default != null
|
||||||
presence: column.is_nullable === "NO",
|
const hasNextVal =
|
||||||
}
|
|
||||||
const hasDefault =
|
|
||||||
typeof column.column_default === "string" &&
|
typeof column.column_default === "string" &&
|
||||||
column.column_default.startsWith("nextval")
|
column.column_default.startsWith("nextval")
|
||||||
const isGenerated =
|
const isGenerated =
|
||||||
column.is_generated && column.is_generated !== "NEVER"
|
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] = {
|
tables[tableName].schema[columnName] = {
|
||||||
autocolumn: isAuto,
|
autocolumn: isAuto,
|
||||||
name: columnName,
|
name: columnName,
|
||||||
|
|
Loading…
Reference in New Issue