Repository navigation
Description
Issue type:
[X] question
[X] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ] cordova
[X] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[X] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Hi Guys,
I'm trying to create a many to many relation in typeorm with mongodb.
I have Application entity and Category entity.
Application can have multiple categories and Category can have multiple applications.
I followed all examples and samples in typeorm repo and in the internet but yet i fail to succeed.
This are the models and how i create it
Application Entity:
@Entity()
export class Application {
@ObjectIdColumn() id: ObjectID;
@Column() name: string;
@Column() description: string;
@ManyToOne(type => User, user => user.applications)
@Column() advertiser: string;
@Column() previewLink: string;
@Column() appId: string;
@OneToOne(type => OsType)
@JoinColumn()
osType: OsType;
@OneToMany(type => OsVersion, osVersion => osVersion.application)
osVersions: OsVersion[];
@ManyToMany(type => AppCategory, category => category.applications, {
cascade: true,
primary: true,
eager: true,
})
@JoinTable({
name: 'applications_app_category', // table name for the junction table of this relation
joinColumn: {
name: 'app_category',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'applications',
referencedColumnName: 'id',
},
})
app_category: AppCategory[];
@Column() subCategory: string;
@Column() icon: string;
@Column() creatives: string;
}
Category Entity:
@Entity()
export class AppCategory {
@ObjectIdColumn() id: ObjectID;
@Column() title: string;
@Column() shopIdentifier: string;
@Column() shopParentIdentifier: string;
@ManyToMany(type => Application, application => application.app_category)
applications: Application[];
@OneToOne(type => OsType)
@JoinColumn()
osType: OsType;
}
As Explained on repo documentation i do not save the new category i create because the cascade should do it.
Category create code :
const category = await this.appCategoryRepository.findOne({where: {shopIdentifier: appData.genreId} , relations: ['applications']});
if (category === undefined) {
const newCat = new AppCategory();
newCat.shopIdentifier = appData.genreId;
newCat.title = appData.genre;
newCat.osType = await this.osTypeRepository.findOne({where: {name: platformName}});
if (appData.genreId.includes('_')) {
newCat.shopParentIdentifier = appData.genreId.split('_')[0];
}
return newCat;
} else {
return category;
}
Application create code :
const application = new Application();
application.appId = bundle;
application.name = appData.title;
application.previewLink = url;
application.osType = platformObj;
application.icon = await this.googleStorageService.uploadAppIcon(appData.icon, bundle + '.png', bundle);
application.description = appData.summary;
application.app_category = [category];
const newApp = await this.applicationRepository.save(application).catch(e => {
console.log(e);
});
The result:
when the code is running in the save row i get an error like this:
TypeError: Cannot read property '_id' of undefined
at docs.map.doc (/Users/tsahk/Development/ShiftAdminServer/node_modules/mongodb/lib/operations/collection_ops.js:1110:45)
at Array.map (<anonymous>)
at prepareDocs (/Users/tsahk/Development/ShiftAdminServer/node_modules/mongodb/lib/operations/collection_ops.js:1109:15)
at Collection.insertMany (/Users/tsahk/Development/ShiftAdminServer/node_modules/mongodb/lib/collection.js:520:10)
at MongoQueryRunner.<anonymous> (/Users/tsahk/Development/ShiftAdminServer/src/driver/mongodb/MongoQueryRunner.ts:275:57)
at step (/Users/tsahk/Development/ShiftAdminServer/node_modules/tslib/tslib.js:133:27)
at Object.next (/Users/tsahk/Development/ShiftAdminServer/node_modules/tslib/tslib.js:114:57)
at /Users/tsahk/Development/ShiftAdminServer/node_modules/tslib/tslib.js:107:75
at new Promise (<anonymous>)
at Object.__awaiter (/Users/tsahk/Development/ShiftAdminServer/node_modules/tslib/tslib.js:103:16)
Tough on the mongo i can see that both Collections are created and on the application the relation is okay you can see it in the attachment but on the cateogry collection the row is inserted but there is no reverse relation to application.
I wasted hours and thousands of tries and searching the internet plus upgrading mongo it self and the typeorm to latest. still now luck.
Your help will save me :)
Thank you !


Activity
rustamwin commentedon Mar 13, 2019
mongo driver does not support relations (for now). only embeddeds
kashkashio commentedon Mar 13, 2019
Thanks for the fast reply !
So what is the best practice to acheive what i'm trying to do ?
rustamwin commentedon Mar 13, 2019
I think for many to many relations you have to do it yourself, in rest of relations you can use embeddeds
Kononnable commentedon Mar 19, 2019
Looks like issue was resolved.