Enum not resolved if it is from an external file
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
I am using nestjs, I have defined an enum in an entity and used there. I needed to use it also in another entity, however, using an import typeorm seems to not resolve it.
In my file order.entity.ts
I define
export enum OrderStatus {
placed = "placed",
paid = "paid",
confirmed = "confirmed",
shipped = "shipped",
completed = "completed",
cancelled = "cancelled"
}
@Entity()
export class Order extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
id: string
@IsNotEmpty()
@Column({ type: "enum", enum: OrderStatus })
status: OrderStatus
}
and in the issue entity order-product.entity.ts
I write:
import { OrderStatus } from "./order.entity";
@Entity()
export class OrderProduct extends BaseEntity {
@PrimaryGeneratedColumn("increment")
id: number
@IsNotEmpty()
@Column({ type: "enum", enum: OrderStatus })
status: OrderStatus
}
The resulting query is CREATE TABLE `order_product` (`id` int NOT NULL AUTO_INCREMENT, `status` enum NOT NULL, PRIMARY KEY (`id`))
producing an sql error as the enum is not defined.
The error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL, PRIMARY KEY (`id`))' at line 1
Defining the enum also inside the orderProduct entity file the enum is resolved and the query is executed correctly.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5
Okay, I checked @jeznag and know what is the issue. The issue is in JavaScript runtime behavior. You are circular referencing files, and
ActionMeta
is importingAction
whileAction
importsActionMeta
. And unlucky enum is undefined in this circular mess. While classes somehow work (also using “hacks”, like () => ClassDefinition), it looks like this is a dead case for enum. Such circular references always bring problems.To ensure I’m right, just console.log() your enum inside
ActionMeta
after class definition.This issue can’t be resolved, because it’s related to JavaScript runtime behavior. Solution: extract enum to a separate file with only enum(s) definitions.
This behaviour happens here as well. While in version 0.2.29 this was not happening, recently we upgraded to 0.2.34 and it is impossible to run migration:generate without duplicating the enun in each entity that uses it.
I found that the
isArraysEqual
method on this case passes the first enum array as undefined, while the second one is correct. As undefined is different than an array this method fails.This bug affects also [X] postgres