mysql2sqlite icon indicating copy to clipboard operation
mysql2sqlite copied to clipboard

Multi-line constraint causes syntax error

Open juliniq opened this issue 8 years ago • 2 comments

On exporting a database schema, MySQL Workbench (6.3.10) dumps a single CONSTRAINT in CREATE TABLE on multiple lines:

-- MySQL Script generated by MySQL Workbench
-- Wed Nov 29 14:10:23 2017
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Table `test_parent`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_parent` ;

CREATE TABLE IF NOT EXISTS `test_parent` (
  `id` INT NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_child`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_child` ;

CREATE TABLE IF NOT EXISTS `test_child` (
  `id` INT NOT NULL,
  `test_parent_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_test_child_test_parent`
    FOREIGN KEY (`test_parent_id`)
    REFERENCES `test_parent` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
) ENGINE = InnoDB;

CREATE INDEX `fk_test_child_test_parent_idx` ON `test_child` (`test_parent_id` ASC);


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

(Output adapted regarding ) ENGINE = InnoDB lines (see Issue #24))

mysql2sqlite outputs:

 ./mysql2sqlite parentchild_dump.sql
PRAGMA synchronous = OFF;
PRAGMA journal_mode = MEMORY;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `test_parent` (
  `id` integer NOT NULL
,  PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `test_child` (
  `id` integer NOT NULL
,  `test_parent_id` integer NOT NULL
,  PRIMARY KEY (`id`)
,  CONSTRAINT `fk_test_child_test_parent`
,    FOREIGN KEY (`test_parent_id`)
,    REFERENCES `test_parent` (`id`)
,    ON DELETE NO ACTION
,    ON UPDATE NO ACTION
);
CREATE INDEX `fk_test_child_test_parent_idx` ON `test_child` (`test_parent_id` ASC);
END TRANSACTION;
WARN Pure sqlite identifiers are case insensitive (even if quoted
     or if ASCII) and doesnt cross-check TABLE and TEMPORARY TABLE
     identifiers. Thus expect errors like "table T has no column named F".

Apparently mysql2sqlite inserts a comma for each new line, causing a syntax error on SQLite3:

$ ./mysql2sqlite ../../../Spielwiese/parentchild_testdump_fix.sql | sqlite3
WARN Pure sqlite identifiers are case insensitive (even if quoted
     or if ASCII) and doesnt cross-check TABLE and TEMPORARY TABLE
     identifiers. Thus expect errors like "table T has no column named F".
memory
Error: near line 8: near ",": syntax error
Error: near line 18: no such table: main.test_child

juliniq avatar Nov 29 '17 13:11 juliniq

I found a plugin (mysql-wb-exportsqlite) that converts MySQL Workbench models to SQLite3 directly, so probably I won't come across at this issue with mysql2sqlite anymore.

juliniq avatar Nov 29 '17 14:11 juliniq

@juliniq please refer to my argumentation in https://github.com/dumblob/mysql2sqlite/issues/24#issuecomment-347993916 . Should you have any better idea, I'm keen to hear you out.

dumblob avatar Nov 29 '17 21:11 dumblob