Fixing duplicate ID errors with typeorm-factory

Published under TypeScript.

When creating a factory with typeorm-factory, you have to make sure any dynamic data is handled within a closure, otherwise it'll only run once when the factory is imported.

If you define a factory like the above, the faker.lorem.word() will only run once, when the factory is imported. This means if you were expecting a random word for each instance the factory creates, you won't get it. Instead, each factory will have the exact same word.

To fix this, you must use a sequence instead of attr:

The sequence will accept a closure as the second argument, which will be invoked whenever the factory creates a new instance.

Thanks for reading!

Did you find this post useful? Let me know on Twitter! If you found an issue with the content, submit a pull request!

Subscribe to my newsletter to know when I publish more content in the future.

❤️ Likes: 0
📣 Retweets: 0
💬 Replies: 0
🙊 Mentions: 0