我当前正在尝试使用不带Typescript的TypeORM创建Many To One
和One to Many
关系。
我设置了以下实体:
./ entity / proposal
module.exports = {
name: "Proposal",
columns: {
id: {
primary: true,
type: "int",
generated: true
},
name: {
type: "varchar",
length: 60
},
description: {
type: "varchar",
default: "",
length: 150
},
createdAt: {
createDate: true
},
updatedAt: {
updateDate: true
}
},
relations: {
sections: {
target: "Section",
type: "one-to-many",
treeParent: true,
cascade: true,
inverseSide: 'Proposal'
}
}
}
./实体/部分
module.exports = {
name: "Section",
columns: {
id: {
primary: true,
type: "int",
generated: true
},
name: {
type: "varchar",
length: 60,
default: "New Section"
},
order: {
type: "int"
}
},
relations: {
proposal: {
target: "Proposal",
type: "many-to-one",
joinColumn: true,
nullable: false
}
}
}
在这里,我创建了两个函数,一个用于插入投标,另一个用于创建模拟数据,该函数使用级联功能插入行(根据此示例:https://github.com/typeorm/javascript-example)
>./ controller / createProposal.js
const getRepository = require('typeorm').getRepository
const createProposal = async proposal => {
let proposalRepository = getRepository("Proposal");
let result = await proposalRepository.save(proposal)
.then( savedProposal => {
return savedProposal
})
.catch( err => {
return {
message: "There was an issue saving the proposal",
err: err
}
});
return result
}
module.exports = createProposal
./ controller / mockData.js
const createProposal = require('../proposal/createProposal')
const insertMockData = async () => {
let section1 = {
name: "Letter",
order: 1
}
let section2 = {
name: "Cloud",
order: 2
}
let proposal = {
name: "Cloud Proposal",
description: "Cloud Proposal",
sections: [
section1, section2
]
}
let result = await createProposal(proposal)
}
module.exports = insertMockData
将insertMockData()
函数与nullable: true
结合使用会导致以下结果:
=== Proposal Table ===
id name description
1 "Cloud Proposal" "Cloud Proposal"
=== Section Table ===
id name order proposalid
1 "Letter" 1 null
2 "Cloud" 2 null
我需要在分区表中显示proposalid
,以便我急切地希望通过proposalRepository.findOneOrFail({ id , relations: ['sections'] })
找到它。
我在做什么错了?
答案 0 :(得分:0)
尝试将eager: true
添加到Section->proposal
relations: {
proposal: {
target: "Proposal",
type: "many-to-one",
joinColumn: true,
nullable: false,
eager: true // This line
}
}
答案 1 :(得分:0)
希望这对某人有用。
我最近在使用TypeORM和NodeJS(版本one-to-many
)的many-to-one
和0.2.18
关系上遇到了相同的问题。
这是具有工作关系的工作实体:
const { EntitySchema } = require('typeorm')
const FirstEntity = require('../model/FirstEntity')
module.exports = new EntitySchema({
name: 'FirstEntity',
target: FirstEntity,
columns: {
id: {
primary: true,
type: 'bigint',
generated: true,
},
createdAt: {
type: 'timestamp with time zone',
default: () => 'NOW()',
},
updatedAt: {
type: 'timestamp with time zone',
default: () => 'NOW()',
},
...........
},
relations: {
secondentities: {
target: 'SecondEntity',
type: 'one-to-many',
cascade: true,
inverseSide: 'firstEntity',
eager: true,
nullable: false,
},
},
})
const { EntitySchema } = require('typeorm')
const SecondEntity = require('../model/SecondEntity')
module.exports = new EntitySchema({
name: 'SecondEntity',
target: SecondEntity,
columns: {
id: {
primary: true,
type: 'bigint',
generated: true,
},
createdAt: {
type: 'timestamp with time zone',
default: () => 'NOW()',
},
updatedAt: {
type: 'timestamp with time zone',
default: () => 'NOW()',
},
...........
},
relations: {
firstEntity: {
target: 'FirstEntity',
type: 'many-to-one',
inverseSide: 'secondentities',
nullable: false,
},
},
})
我的第一个问题是.save()
中的firstEntity
根本没有保存嵌套的secondentities
。它是由cascade
属性解决的。
添加cascade
之后,我的第二个问题是保存在数据库中的firstEntity
中缺少保存的id
secondentities
。通过添加inverseSide
属性来解决。
请注意,nullable
属性根本不起作用,因此在.save()
之后,我不得不手动.findOneOrFail
保存的firstEntity
并检查{{1 }}存在。