【发布时间】:2020-11-14 14:58:00
【问题描述】:
我是 NestJS/TypeORM 的新手,请见谅。
我创建了多对多关系;我的表是使用正确的列自动创建的。
我有一个可以有很多用户的位置,而一个用户可以有很多位置。
我的路线是这样的:
http://localhost:3000/locations/:location-id/users
我的location.entity.ts 看起来像这样:
@ManyToMany(type => User, user => user.locations, { eager: true })
@JoinTable()
users: User[];
我的user.entity.ts 看起来像这样:
@ManyToMany(type => Location, location => location.users, { eager: false })
locations: Location[];
location_users_user 使用这些列生成表:
locationId | userId
到目前为止,一切看起来都很棒!当我使用 Postman 向我的路由发送 GET 请求时,我在控制台中看到此错误:
column location_users_user.locationid does not exist
当我的列名是 locationId 时,我看到 locationid 就是它要查找的内容。有什么地方需要设置列名的大小写吗?
我还通过this SO thread 在JoinTable 装饰器中设置了额外的参数。
剩下的就是这个:
// location.entitiy.ts
@ManyToMany(type => User, user => user.locations, { eager: true })
@JoinTable({
name: 'location_user',
joinColumn: {
name: 'locationId',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'userId',
referencedColumnName: 'id',
},
})
users: User[];
但是,我仍然收到此错误:
column location_users_user.locationid does not exist
我认为我设置的 Join 或其他内容不正确。我的位置实体上只有那个装饰器。
感谢您的任何建议!
编辑
我已将我的user.repository.ts 文件更新如下:
async getLocationUsers(locationId: number): Promise<User[]> {
const query = this.createQueryBuilder('location_user')
.where('location_user.locationId = :locationId', { locationId });
错误仍然认为我正在寻找locationid 列。我已将其更改为foo,只是为了看看我是否在正确的位置,我是。我不确定为什么缺少locationId 的情况。
EDIT2
我发现这可能是 Postgres 的事情?使用双引号,我现在在错误中看到正确的表/列名称:
const query = this.createQueryBuilder('location_user')
.where('location_user."locationId" = :locationId', { locationId });
结果:column location_user.locationId does not exist
这仍然很奇怪,因为该表确实存在,列也存在。
编辑
这是location.entity.ts 文件:
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(type => User, user => user.locations, { eager: true })
@JoinTable()
users: User[];
这是user.entity.ts 文件:
@PrimaryGeneratedColumn()
id: number;
@Column()
email: string;
@ManyToMany(type => Location, location => location.users, { eager: false })
locations: Location[];
当我得到一个特定的位置时,我能够看到用户的关系,所以我知道它工作正常。我正在尝试获取属于该位置的所有用户;这是我的user.repository.ts 文件的样子:
async getLocationUsers(locationId: number): Promise<User[]> {
const query = this.createQueryBuilder('user')
.where('location_users_user."locationId" = :locationId', { locationId });
});
try {
return await query.getMany();
} catch (e) {
console.log('error: ', e);
}
}
【问题讨论】:
-
能否请您添加两个实体的完整代码?
-
嗨@yash 我刚刚在底部更新了我的问题,以包括关系和我的存储库方法。谢谢!