如何使用Nestjs和TypeOrm定义多对多列?

如何使用Nestjs和TypeOrm定义多对多列?,nestjs,typeorm,Nestjs,Typeorm,我是nestjs/typeorm的新手,请原谅我 我建立了一种多对多的关系;“我的表”是使用正确的列自动创建的 我有一个可以有很多用户的位置,一个用户可以有很多位置 我的路线如下所示: http://localhost:3000/locations/:location-id/用户 我的location.entity.ts如下所示: @ManyToMany(type=>User,User=>User.locations,{eager:true}) @JoinTable() 用户:用户[]; 我

我是nestjs/typeorm的新手,请原谅我

我建立了一种多对多的关系;“我的表”是使用正确的列自动创建的

我有一个可以有很多用户的位置,一个用户可以有很多位置

我的路线如下所示:

http://localhost:3000/locations/:location-id/用户
我的
location.entity.ts
如下所示:

@ManyToMany(type=>User,User=>User.locations,{eager:true})
@JoinTable()
用户:用户[];
我的
user.entity.ts
如下所示:

@ManyToMany(type=>Location,Location=>Location.users,{eager:false})
地点:地点[];
location\u users\u user
将使用以下列生成表:

locationId | userId
到目前为止,一切看起来都很棒!当我使用邮递员向我的路线发送
GET
请求时,我在控制台中看到以下错误:

column location_users_user.locationid does not exist 
当我的列名为
locationid
时,我发现它正在寻找
locationid
。是否有地方需要设置列名的大小写

我还在
JoinTable
decorator中设置了额外的参数

这让我想到:

//location.entity.ts
@ManyToMany(type=>User,User=>User.locations,{eager:true})
@可接合({
名称:“位置\用户”,
joinColumn:{
名称:'locationId',
referencedColumnName:'id',
},
反向连接列:{
名称:'userId',
referencedColumnName:'id',
},
})
用户:用户[];
但是,我仍然会遇到以下错误:

column location_users_user.locationid does not exist 
我认为我没有设置正确的
连接
或其他什么。我的位置实体上只有那个装饰师

谢谢你的建议

编辑

我已更新了我的
user.repository.ts
文件,如下所示:

async getLocationUsers(locationId:number):承诺{
const query=this.createQueryBuilder('location\u user')
.where('location_user.locationId=:locationId',{locationId});
错误仍然认为我在查找
locationid
列。我已将其更改为
foo
,以查看我是否在正确的位置,我是否在正确的位置。我不确定为什么它缺少
locationid
的情况

EDIT2

我发现这可能是Postgres的问题?使用双引号,我现在在错误中看到了正确的表/列名:

const query=this.createQueryBuilder('location\u user'))
.where('location_user..locationId=:locationId',{locationId});
结果为:
列位置\u user.locationId不存在
这仍然很奇怪,因为该表和列都存在

编辑

这是
location.entity.ts
文件:

@PrimaryGeneratedColumn()
id:编号;
@第()列
名称:字符串;
@ManyToMany(type=>User,User=>User.locations,{eager:true})
@JoinTable()
用户:用户[];
以下是
user.entity.ts
文件:

@PrimaryGeneratedColumn()
id:编号;
@第()列
电子邮件:字符串;
@ManyToMany(type=>Location,Location=>Location.users,{eager:false})
地点:地点[];
当我得到一个特定的位置时,我能够看到用户之间的关系,因此我知道这工作正常。我正在尝试只获取属于该位置的所有用户;下面是我的
user.repository.ts
文件的外观:

async getLocationUsers(locationId:number):承诺{
const query=this.createQueryBuilder('用户')
.where('location\u users\u user..locationId=:locationId',{locationId});
});
试一试{
return wait query.getMany();
}捕获(e){
log('error:',e);
}
}

我刚刚用
@manytomy
检查了我自己的一些代码,我能发现的只有以下两个区别:

第一最佳赌注 在我的存储库中,使用联接列调用TypeOrm的
createQueryBuilder
方法时,有两个不同之处:

  • 我使用一个
    leftJoin
    (可能是另一个,取决于您的用例逻辑和数据库设计)来检索链接表
  • 要筛选结果的FK字段上没有双引号
  • user.repository.ts
    中的
    getLocationUsers
    方法代码将导致以下结果:

    async getLocationUsers(locationId:number):承诺{
    const query=this.createQueryBuilder('用户')
    .leftJoin('user.locations','location')
    .where('location.id=:locationId',{locationId});
    });
    试一试{
    return wait query.getMany();
    }捕获(e){
    log('error:',e);
    }
    }
    
    第二个,以防第一个不能解决你的问题
    @ManyToMany(type=>Location,Location=>Location.users,{eager:false})
    
    当我使用

    @ManyToMany(()=>Location,(Location)=>Location.users,{eager:false})
    
    这种差异(不要使用
    类型=>
    ,而是
    ()=>
    )会改变什么吗?老实说,我不这么认为,可能是一些语法上的甜点(或者不——待证实,这只是假设)


    希望有帮助,让我知道:)

    我刚刚用
    @ManyToMany
    检查了我自己的一些代码,我能发现的唯一两个区别是:

    第一最佳赌注 在我的存储库中,使用联接列调用TypeOrm的
    createQueryBuilder
    方法时,有两个不同之处:

  • 我使用一个
    leftJoin
    (可能是另一个,取决于您的用例逻辑和数据库设计)来检索链接表
  • 要筛选结果的FK字段上没有双引号
  • 将生成
    user.repository.ts
    中的
    getLocationUsers
    方法的代码