Kononnable / typeorm-model-generator

Generates models for TypeORM from existing database.
MIT License
1.51k stars 281 forks source link

Didn't Found Entity Column Error #191

Open NickClicknClear opened 5 years ago

NickClicknClear commented 5 years ago

Just testing out this tool with a simple Maria database with User, Role and User_Role tables with a many to many relationship and I got this error.

typeorm-model-generator@0.3.4
[18:20:49] Starting creation of model classes.
Relation between tables User_Role and User didn't found entity column User_Role.undefined.
Error occured in typeorm-model-generator.
typeorm-model-generator@0.3.4  node@v12.6.0
If you think this is a bug please open an issue including this log on https://github.com/Kononnable/typeorm-model-generator/issues
Error
    at Object.LogError (/Users/nicklong/.npm/_npx/72072/lib/node_modules/typeorm-model-generator/dist/src/Utils.js:10:21)
 at /Users/nicklong/.npm/_npx/72072/lib/node_modules/typeorm-model-generator/dist/src/drivers/AbstractDriver.js:156:31
    at Array.forEach (<anonymous>)

I think it's expecting some kind of naming convention, but I'm not sure what's expected?

Kononnable commented 5 years ago

Can you please provide DDL statements for your database? I have to reproduce the issue to know what's going wrong. Tests pass on mariadb, so it looks like it's some specific case.

NickClicknClear commented 5 years ago

Thanks for looking into this.

The table I ran the script against looked like this:


# Dump of table Role
# ------------------------------------------------------------

DROP TABLE IF EXISTS `Role`;

CREATE TABLE `Role` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

# Dump of table User
# ------------------------------------------------------------

DROP TABLE IF EXISTS `User`;

CREATE TABLE `User` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `email` varchar(255) NOT NULL DEFAULT '',
  `created` datetime NOT NULL,
  `password` varchar(255) NOT NULL DEFAULT '',
  `isActive` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

# Dump of table User_Role
# ------------------------------------------------------------

DROP TABLE IF EXISTS `User_Role`;

CREATE TABLE `User_Role` (
  `userID` int(11) unsigned NOT NULL,
  `roleID` int(11) unsigned NOT NULL,
  PRIMARY KEY (`userID`,`roleID`),
  KEY `RoleID` (`roleID`),
  CONSTRAINT `user_role_ibfk_1` FOREIGN KEY (`UserID`) REFERENCES `User` (`id`),
  CONSTRAINT `user_role_ibfk_2` FOREIGN KEY (`roleID`) REFERENCES `Role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Let me know if you need any further information.

Kononnable commented 5 years ago

I couldn't reproduce your issue - it generated proper entities for me. I used just simple model generation, without additional flags which might be the reason(npx typeorm-model-generator@v0.3.4 -h localhost -e mariadb -d test -u root -x admin -p 3307).

Which exactly db version are you using? Are you using any additional flags, or just simple model generation?

NickClicknClear commented 5 years ago

I've done a bit more digging and I can reproduce the issue reliably though I'm not really sure why it is happening.

I don't think it's anything to do with my specific schema.

If I create a new database and import the SQL I can run:

npx typeorm-model-generator -h localhost -e mariadb -d test -u root -x password -p 3306

and it works fine.

If I then rename the database to test2 using SequelPro and run

npx typeorm-model-generator -h localhost -e mariadb -d test2 -u root -x password -p 3306

It will then fail with the error: Relation between tables User_Role and user didn't found entity model user.

So I think the issue might be Sequel Pro doing something weird to the database if you rename it although I don't know quite what.

I can now easily work around this issue so I'm happy for you to close, but I thought I'd provide you with the information in case you wanted to look into it further.

Kononnable commented 5 years ago

Probably renaming database(tool doesn't matter) produces this bug. I will keep this open till I have some more time to fix the issue.