Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding an associated key still creates a new instance in the database #35

Closed
dkpoult opened this issue Jul 26, 2024 · 5 comments
Closed
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed

Comments

@dkpoult
Copy link

dkpoult commented Jul 26, 2024

Hey there, firstly awesome revival of this package, it's made E2E testing our systems so much smoother than previous approached! But we've recently run into a bug/issue with creating instances:

In the create method of the Factory class, the method first gets the default attributes and resolves the associations before taking into account associated keys manually passed in the overrides field. This lands up creating undesired entries in the DB as you'd expect it to just link to the provided foreign key, but instead it lands up linking the provided key and creating an loose hanging entry in the table as well.

I feel the desired behaviour would be to merge the default attributes and overrides and only afterwards resolve the associations. We noticed this because we have unique constraints on some of our tables and tests were failing because sometimes the factory would happen to create a new entry with the same unique fields and the suite would error out.

Happy to try make a PR for the desired change if this project is still being maintained!

@thiagomini
Copy link
Owner

thiagomini commented Jul 26, 2024

Hello @dkpoult , thanks for the enthusiasm and for reporting the bug :)

PRs are more than welcome, for sure! I haven't had much time lately to work on it due to my full-time job, but I certainly want to improve it further :)

Btw, which ORM and database are you using?

@thiagomini thiagomini added bug Something isn't working enhancement New feature or request help wanted Extra attention is needed labels Jul 26, 2024
@dkpoult
Copy link
Author

dkpoult commented Jul 29, 2024

@thiagomini we're using Sequelize+Sequelize-Typescript with a Postgres database.

I've created PR #36 to address the issue with a new test case for it, hope it's satisfactory! :)

@thiagomini
Copy link
Owner

Hey @dkpoult , thanks for the contribution, I'll review it as soon as possible and merge if everything looks good!

@thiagomini
Copy link
Owner

thiagomini commented Aug 14, 2024

New version released @dkpoult : https://www.npmjs.com/package/factory-girl-ts/v/2.3.1

Again, Thank you very much for your contribution :)

@dkpoult
Copy link
Author

dkpoult commented Aug 15, 2024

Super, thank you 🥳 glad I could help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants