Open juanzgc opened 2 years ago
With that being said, i'm not quite sure of the fix to this issue. We should be able to use TypeORM's Generate Migration capabilities considering it is an ORM, and we are already doing this in Core MedusaJS.
But our issue here is with importing the Models from Medusa Core into the Extender and still having the same capabilities of generating a migration.
I've just tried to reproduce, and I did not get any error. here is a screen shot of the output
As discussed, it seams to come from typeorm driver postgres during changes check. So it seams to be linked to typeorm not managing properly the checks
https://github.com/medusajs/medusa/issues/2054#issuecomment-1217660882
Adding a few more details per our discussion:
This will require updating to TypeORM v3
which includes breaking changes that will need to be addressed beforehand. Changes will include and are not limited to: connection, repositories, queries, options, etc...
At the moment it seems that TypeORM can't distinguish between entities from different repositories which results in the issue that we're seeing.
I fixed this issue by using .env and add these lines
TYPEORM_ENTITIES=./node_modules/@medusajs/medusa/dist/models/*.js,./dist/models/*.js TYPEORM_MIGRATIONS=./node_modules/@medusajs/medusa/dist/migrations/*.js,./dist/migrations/*.js
typeorm is unable to find medusa core models that's why giving you the error above and keep sure you use medusa ver 1.3.5
@juanzgc did you give it a shot 👆
I have not, will attempt this later tonight 👀
@aboozaid where did you put your .env file in medusa-store? can you please give me a path? thanks
@aboozaid I've added the following in the .env
(my folder structure is a bit different):
TYPEORM_ENTITIES=./node_modules/@medusajs/medusa/dist/models/*.js,./dist/modules/**/*.entity.js
TYPEORM_MIGRATIONS=./node_modules/@medusajs/medusa/dist/migrations/*.js,./dist/modules/migrations/*.js
however, I'm still seeing the same errors. I think it's worth mentioning, I can add the following in ormconfig.js
:
"entities": [
"./src/modules/**/*{.js,.ts}",
"node_modules/@medusajs/medusa/dist/models/**/*{.js,.ts}"
],
"migrations": [
"./src/migrations/**/*.migration{.js,.ts}",
"node_modules/@medusajs/medusa/dist/migrations/**/*{.js,.ts}"
],
and even though migrations will now always generate, they won't generate correctly. It will attempt to drop indices and foreign keys that shouldn't be dropped.
Are you using a ormconfig.js
file? What is the difference between adding those values via a .env
file vs ormconfig.js
file? If you don't mind, would it be possible to provide a more detailed solution?
I'm not sure if @adrien2p, was able to also give it a shot. But personally, it didn't seem to work on my end. cc @adrien2p
@aboozaid where did you put your .env file in medusa-store? can you please give me a path? thanks
I have a monorepo project so I put it in the root
@aboozaid I've added the following in the
.env
(my folder structure is a bit different):TYPEORM_ENTITIES=./node_modules/@medusajs/medusa/dist/models/*.js,./dist/modules/**/*.entity.js TYPEORM_MIGRATIONS=./node_modules/@medusajs/medusa/dist/migrations/*.js,./dist/modules/migrations/*.js
however, I'm still seeing the same errors. I think it's worth mentioning, I can add the following in
ormconfig.js
:"entities": [ "./src/modules/**/*{.js,.ts}", "node_modules/@medusajs/medusa/dist/models/**/*{.js,.ts}" ], "migrations": [ "./src/migrations/**/*.migration{.js,.ts}", "node_modules/@medusajs/medusa/dist/migrations/**/*{.js,.ts}" ],
and even though migrations will now always generate, they won't generate correctly. It will attempt to drop indices and foreign keys that shouldn't be dropped.
Are you using a
ormconfig.js
file? What is the difference between adding those values via a.env
file vsormconfig.js
file? If you don't mind, would it be possible to provide a more detailed solution?I'm not sure if @adrien2p, was able to also give it a shot. But personally, it didn't seem to work on my end. cc @adrien2p
Add these to .env file
TYPEORM_CONNECTION=postgres
TYPEORM_URL=postgres://localhost/your_database_name
when I migrate or create migrations I always use typeorm commands without any ormconfig.js file as I put all configs in .env file and typeorm automatically read from it
I can confirm this doesn't work on my end. The migration itself will generate but it will be full of constraint removals that are completely incorrect and not what we expect.
As expected, there isn't a difference between ormconfig.js
and using environment variables. Atleast from what I've tested locally.
@aboozaid Would it be possible to see a live demo of you generating migrations? Could you try extending the Store
entity. Some entities, for example User
which don't have any referencing entities will actually generate without a problem. The problem begins with entities that do have references. Would be good to possibly get a live demo if you are available.
It seems that in Typeorm 3 - this solution might work but we will have to wait until Medusa is migrated to use Typeorm v3: https://github.com/typeorm/typeorm/issues/9122#issuecomment-1160347397
@adrien2p I don't think the issue is fixed yet, as we are still facing this issue. can you please let it open so that their might be a solution shared by team. 😊
Thanks
Sorry, i closed it because it is more related to typeorm and did not see any activities 😅
Also, i ve put on hold the migration to typeorm 3 in the core as it was collided a lot with our feature rate 🥹
@adrien2p There is an issue when 2 modules override the same entity, i believe the issue in databaseLoader
function any recommendations?
Describe the bug Generating a migration results in errors if the Entity that you are overriding has column types of other Entities (that are imported).
For example:
images
,options
,variants
,profile
,collection
,type
,tags
,sales_channel
]Whereas User doesn't face this error because there are no column types of other imported entities.
It seems that for whatever reason, when generating the migration with extended Entities, typeorm has difficulty with nested imports.
@medusajs/medusa/dist/models/product.d.ts
:@medusajs/medusa/dist/models/user.d.ts
:To Reproduce Steps to reproduce the behavior:
ormconfig.json
package.json
:typeorm
:Entity metadata for Product#images was not found
. However, if you add anexport
to the Product Entity extender you won't see the same error. The error will now beEntity metadata for Product#options was not found
.export { Image } from '@medusajs/medusa/dist/models';
@MedusaEntity({ override: MedusaProduct }) @Entity() export class Product extends MedusaProduct { @Index() @Column({ nullable: false }) store_id: string; }
TypeORMError: Entity metadata for Product#options was not found. Check if you specified a correct entity object and if it's connected in the connection options.