Merge pull request #1923 from Budibase/feature/sql-auto-columns

SQL Auto columns
This commit is contained in:
Michael Drury 2021-07-06 10:33:32 +01:00 committed by GitHub
commit 3decb17e6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 15 deletions

View File

@ -72,9 +72,7 @@
{#if isUsersTable} {#if isUsersTable}
<EditRolesButton /> <EditRolesButton />
{/if} {/if}
{#if isInternal} <HideAutocolumnButton bind:hideAutocolumns />
<HideAutocolumnButton bind:hideAutocolumns />
{/if}
<!-- always have the export last --> <!-- always have the export last -->
<ExportButton view={$tables.selected?._id} /> <ExportButton view={$tables.selected?._id} />
{/if} {/if}

View File

@ -1,9 +1,22 @@
CREATE DATABASE IF NOT EXISTS main; CREATE DATABASE IF NOT EXISTS main;
USE main; USE main;
CREATE TABLE Persons ( CREATE TABLE Persons (
PersonID int NOT NULL PRIMARY KEY, PersonID int NOT NULL AUTO_INCREMENT,
LastName varchar(255), LastName varchar(255),
FirstName varchar(255), FirstName varchar(255),
Address varchar(255), Address varchar(255),
City varchar(255) City varchar(255),
PRIMARY KEY (PersonID)
); );
CREATE TABLE Tasks (
TaskID int NOT NULL AUTO_INCREMENT,
PersonID INT,
TaskName varchar(255),
PRIMARY KEY (TaskID),
CONSTRAINT fkPersons
FOREIGN KEY(PersonID)
REFERENCES Persons(PersonID)
);
INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast');
INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'assembling');
INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing');

View File

@ -0,0 +1,3 @@
#!/bin/bash
docker-compose down
docker volume prune -f

View File

@ -1,14 +1,14 @@
SELECT 'CREATE DATABASE main' SELECT 'CREATE DATABASE main'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
CREATE TABLE Persons ( CREATE TABLE Persons (
PersonID INT NOT NULL PRIMARY KEY, PersonID SERIAL PRIMARY KEY,
LastName varchar(255), LastName varchar(255),
FirstName varchar(255), FirstName varchar(255),
Address varchar(255), Address varchar(255),
City varchar(255) City varchar(255) DEFAULT 'Belfast'
); );
CREATE TABLE Tasks ( CREATE TABLE Tasks (
TaskID INT NOT NULL PRIMARY KEY, TaskID SERIAL PRIMARY KEY,
PersonID INT, PersonID INT,
TaskName varchar(255), TaskName varchar(255),
CONSTRAINT fkPersons CONSTRAINT fkPersons
@ -16,7 +16,7 @@ CREATE TABLE Tasks (
REFERENCES Persons(PersonID) REFERENCES Persons(PersonID)
); );
CREATE TABLE Products ( CREATE TABLE Products (
ProductID INT NOT NULL PRIMARY KEY, ProductID SERIAL PRIMARY KEY,
ProductName varchar(255) ProductName varchar(255)
); );
CREATE TABLE Products_Tasks ( CREATE TABLE Products_Tasks (
@ -30,12 +30,12 @@ CREATE TABLE Products_Tasks (
REFERENCES Tasks(TaskID), REFERENCES Tasks(TaskID),
PRIMARY KEY (ProductID, TaskID) PRIMARY KEY (ProductID, TaskID)
); );
INSERT INTO Persons (PersonID, FirstName, LastName, Address, City) VALUES (1, 'Mike', 'Hughes', '123 Fake Street', 'Belfast'); INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast');
INSERT INTO Tasks (TaskID, PersonID, TaskName) VALUES (1, 1, 'assembling'); INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'assembling');
INSERT INTO Tasks (TaskID, PersonID, TaskName) VALUES (2, 1, 'processing'); INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing');
INSERT INTO Products (ProductID, ProductName) VALUES (1, 'Computers'); INSERT INTO Products (ProductName) VALUES ('Computers');
INSERT INTO Products (ProductID, ProductName) VALUES (2, 'Laptops'); INSERT INTO Products (ProductName) VALUES ('Laptops');
INSERT INTO Products (ProductID, ProductName) VALUES (3, 'Chairs'); INSERT INTO Products (ProductName) VALUES ('Chairs');
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (1, 1); INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (1, 1);
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (2, 1); INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (2, 1);
INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (3, 1); INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (3, 1);

View File

@ -14,6 +14,7 @@ export interface FieldSchema {
relationshipType?: string relationshipType?: string
through?: string through?: string
foreignKey?: string foreignKey?: string
autocolumn?: boolean
constraints?: { constraints?: {
type?: string type?: string
email?: boolean email?: boolean

View File

@ -169,8 +169,13 @@ module MySQLModule {
const constraints = { const constraints = {
presence: column.Null !== "YES", presence: column.Null !== "YES",
} }
const isAuto: boolean =
typeof column.Extra === "string" &&
(column.Extra === "auto_increment" ||
column.Extra.toLowerCase().includes("generated"))
schema[columnName] = { schema[columnName] = {
name: columnName, name: columnName,
autocolumn: isAuto,
type: convertType(column.Type, TYPE_MAP), type: convertType(column.Type, TYPE_MAP),
constraints, constraints,
} }

View File

@ -184,7 +184,11 @@ module PostgresModule {
} }
const type: string = convertType(column.data_type, TYPE_MAP) const type: string = convertType(column.data_type, TYPE_MAP)
const isAuto: boolean =
typeof column.column_default === "string" &&
column.column_default.startsWith("nextval")
tables[tableName].schema[columnName] = { tables[tableName].schema[columnName] = {
autocolumn: isAuto,
name: columnName, name: columnName,
type, type,
} }