how to query 3 tables together?
here are three tables
@Table({
tableName: "published",
timestamps: false,
})
export default class Published extends Model<Published> {
@Column({
autoIncrement: true,
primaryKey: true,
type: DataType.INTEGER,
})
public id: number;
@ForeignKey(() => Teacher)
@Column({
allowNull: false,
type: DataType.INTEGER,
unique: true,
})
public teacherId: number;
@ForeignKey(() => Prototype)
@Column({
allowNull: false,
type: DataType.INTEGER,
unique: true,
})
public prototypeId: number;
@ForeignKey(() => Organization)
@Column({
allowNull: false,
type: DataType.INTEGER,
unique: true,
})
public organizationId: number;
@BelongsTo(() => Teacher)
public teacher: Teacher;
@BelongsTo(() => Prototype)
public prototype: Prototype;
@BelongsTo(() => Organization)
public organization: Organization;
@Column({
allowNull: false,
type: DataType.STRING,
})
public name: string;
@Column({
allowNull: false,
type: DataType.TEXT,
})
public content: string;
}
@Table({
tableName: "organization",
timestamps: false,
})
export default class Organization extends Model<Organization> {
@Column({
autoIncrement: true,
primaryKey: true,
type: DataType.INTEGER,
})
public id: number;
@Column({
allowNull: false,
type: DataType.INTEGER,
unique: true,
})
public userId: number;
@Column({
allowNull: false,
type: DataType.STRING,
})
public name: string;
}
@Table({
tableName: "teacher",
timestamps: false,
})
export default class Teacher extends Model<Teacher> {
@Column({
autoIncrement: true,
primaryKey: true,
type: DataType.INTEGER,
})
public id: number;
@Column({
allowNull: false,
type: DataType.TEXT,
})
public intro: string;
@Column({
allowNull: false,
field: "avatar_url",
type: DataType.STRING,
})
public avatarUrl: string;
@ForeignKey(() => User)
@Column({
allowNull: false,
field: "user_id",
type: DataType.INTEGER,
unique: true,
})
public userId: number;
@BelongsTo(() => User)
public user: User;
}
probably want this effect, how to write
const query={
include: [
{
model: Organization,
as: "organization1",
where:{id:Sequelize.col("Published.organizationId")},
attributes:["id","name"]
},
{
model:Prototype,
as:"prototype1",
where:{id:Sequelize.col("Published.prototypeId")},
attributes:["id","name","content","teacherId"]
}
],
where:{id:id}
}
const published = await Published.findOne(query);
if (published === null) {
throw PUBLISHED_NOT_FOUND;
}
return published;
when calling the return value published.organization1, it is prompted that there is no attribute. What should I do? The rookie asks all the bosses to solve the problem