drawdb-io / drawdb

Free, simple, and intuitive online database diagram editor and SQL generator.
https://drawdb.app
GNU Affero General Public License v3.0
22.7k stars 1.58k forks source link

[BUG] import and get wrong relations #276

Closed saulalexander123 closed 3 weeks ago

saulalexander123 commented 4 weeks ago

Describe the bug when I import a sql script the diagram fails

create database libreria;
use libreria;

-- posibles errores que puede soltar esta tabla
-- 1. usuario existe
-- 2. contraseña incorrecta
-- 3. email ya existe
create table user(
    id int primary key auto_increment,
    username varchar(120) not null,
    email varchar(320) not null unique,
    password varchar(65) not null,
    creation_time datetime default current_timestamp
);
-- Errores de esta tabla
-- 1. No es administrador el usuario 
create table administrators(
    id int primary key auto_increment,
    id_user int not null unique,
    foreign key(id_user) references user(id)
);

create table employee(
    id int primary key auto_increment,
    id_user int not null unique,
    foreign key(id_user) references user(id)
);

CREATE TABLE artista (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(255) NOT NULL,
    descripcion TEXT DEFAULT NULL
);
CREATE TABLE editorial (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(255) NOT NULL,
    direccion VARCHAR(255) DEFAULT NULL
);
CREATE TABLE genero (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(100) NOT NULL
);

CREATE TABLE manga (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(255) NOT NULL,
    id_artista INT DEFAULT NULL,
    id_editorial INT DEFAULT NULL,
    price DECIMAL(10,3) NOT NULL DEFAULT 0.00,
    anio_publicacion YEAR DEFAULT NULL,
    estado ENUM('publicado', 'en publicación', 'cancelado') DEFAULT NULL,
    numero_volumenes INT DEFAULT NULL,
    capitulos INT DEFAULT NULL,
    idioma VARCHAR(100) DEFAULT NULL,
    calificacion DECIMAL(3,2) DEFAULT NULL, -- Puntuación de 0.00 a 10.00
    fecha_inicio DATE DEFAULT NULL,
    fecha_finalizacion DATE DEFAULT NULL,
    imagen_portada VARCHAR(255) DEFAULT NULL,
    FOREIGN KEY (id_artista) REFERENCES artista(id),
    FOREIGN KEY (id_editorial) REFERENCES editorial(id)
);

CREATE TABLE manga_genero (
    id_manga INT NOT NULL,
    id_genero INT NOT NULL,
    PRIMARY KEY (id_manga, id_genero),
    FOREIGN KEY (id_manga) REFERENCES manga(id),
    FOREIGN KEY (id_genero) REFERENCES genero(id)
);
-- no es la mejor opcion

CREATE TABLE manga_images(
    id int primary key auto_increment,
    id_manga int not null,
    url varchar(320) not null,
    foreign key(id_manga) references manga(ID)
);

create table user_manga (
    id int primary key auto_increment,
    id_user int not null unique,
    id_manga int not null,
    foreign key(id_user) references user(id),
    foreign key(id_manga) references manga(id),
    UNIQUE (id_user, id_manga)
);

create table transaction_bougth (
    id int primary key auto_increment,
    id_employee int not null,
    id_manga int not null,
    id_cuantity int not null,
    date_sell datetime not null default current_timestamp,
    foreign key(id_employee) references employee(id),
    foreign key(id_manga) references manga(id)
);

CREATE TABLE logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    user_type ENUM('empleado', 'administrador') NOT NULL,
    action VARCHAR(255) NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    ip_address VARCHAR(45) DEFAULT NULL, -- Puede almacenar IPv4 e IPv6
    details TEXT DEFAULT NULL, -- Información adicional sobre la acción
    FOREIGN KEY (user_id) REFERENCES user(id)
);

To Reproduce Steps to reproduce the behavior: the script sql

  1. import from SQL
  2. drag the script
  3. and look the relations
  4. See error Captura de pantalla 2024-10-24 224149 thats incorrect because these are the true foreign keys

Expected behavior !(the screenshot is from workbench) correct

Desktop (please complete the following information):

Additional context the bug is anoying

ruizhiii commented 3 weeks ago

Issue "foreign key" is not interpreted correctly as "FOREIGN KEY"

Reason current code uses case sensitive string matching

Suggestion use case insensitive matching instead because SQL keywords are case insensitive although they're often written in all caps.

Ref https://stackoverflow.com/questions/153944/is-sql-syntax-case-sensitive

saulalexander123 commented 3 weeks ago

oh thanks, is strange case and is possibe find this issues in studend codes