Currently, I am trying to create my database tables using migrations. I am making use of queryRunner.query not queryRunner.createTable because of this I have multipleStatments set to true in my DataSource. This is the MYSQL raw query I want to run below
CREATE TABLE IF NOT EXISTS app_user(
user_id BIGINT NOT NULL auto_increment,
acc_id VARCHAR(40) DEFAULT (UUID()) UNIQUE NOT NULL,
first_name VARCHAR(35) NOT NULL,
last_name VARCHAR(35),
email VARCHAR(254) UNIQUE NOT NULL,
student_email VARCHAR(254) UNIQUE DEFAULT NULL,
phone_number VARCHAR(11) UNIQUE DEFAULT NULL,
verification_status ENUM('unverified', 'verified') DEFAULT 'unverified',
image_key VARCHAR(250),
is_locked BOOL DEFAULT False,
is_banned BOOL DEFAULT False,
report_count TINYINT CHECK (report_count >= 0 AND report_count <= 4),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(user_id)
);
DELIMITER //
#Trigger to reduce collision
CREATE TRIGGER t_accId BEFORE INSERT ON app_user
FOR EACH ROW
BEGIN
SET new.acc_id := (SELECT SHA1(CONCAT(new.email, RAND(), UUID(), NOW())));
END;
DELIMITER ;
This current query works in MySQLworkbench as expected. However, in my NestJs application, it throws this error.
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //\n"
I tried taking out the DELIMITER clauses in the original sytanx as so and ran it in my NestJs application.
CREATE TABLE IF NOT EXISTS app_user(
user_id BIGINT NOT NULL auto_increment,
acc_id VARCHAR(40) DEFAULT (UUID()) UNIQUE NOT NULL,
first_name VARCHAR(35) NOT NULL,
last_name VARCHAR(35),
email VARCHAR(254) UNIQUE NOT NULL,
student_email VARCHAR(254) UNIQUE DEFAULT NULL,
phone_number VARCHAR(11) UNIQUE DEFAULT NULL,
verification_status ENUM('unverified', 'verified') DEFAULT 'unverified',
image_key VARCHAR(250),
is_locked BOOL DEFAULT False,
is_banned BOOL DEFAULT False,
report_count TINYINT CHECK (report_count >= 0 AND report_count <= 4),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(user_id)
);
#Trigger to reduce collision
CREATE TRIGGER t_accId BEFORE INSERT ON app_user
FOR EACH ROW
BEGIN
SET new.acc_id := (SELECT SHA1(CONCAT(new.email, RAND(), UUID(), NOW())));
END;
It works correctly and when I inspect the table in MySQLworkbench it is correctly reflected. But when I run this same query directly from mysqlworkbench it throws an error in mysqlworkbench. I am assuming this is because of the first occurrence of the ';'.
I have also tried breaking down queryRunner.createTable into two operations but it still fails for the correct syntax.
Is there an appropriate way to create triggers on a table in TypeORM and why does the wrong syntax work in the NestJs application when it fails in MySQLworkbench?