MariaDB关系
MariaDB relationship
我不太明白 sequelize 关联是如何工作的。
我有一个包含两个表的 MariaDB 数据库:公社和 code_postals.
Database schema
code_commune 是 city_code 和 code_postal postal_code。所以,一个城市可以有多个postal_code,多个城市可以共享一个postal_code。
我的 SLQ 请求必须是这样的:
SELECT * FROM code_postals P INNER JOIN communes C ON P.code_commune=C.code_commune;
在我的 TypeScript 数据存储库中 class 我有这段代码:
const Communes = await this.db.define('commune', {
code_commune: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
libelle_commune: {
type: DataTypes.STRING,
allowNull: false
}
});
const Postal = await this.db.define('code_postal', {
code_commune: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
code_postal: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
}
});
// Define the relationship between the both tables
Postal.hasOne(Communes,{foreignKey: 'code_commune'});
Communes.belongsTo(Postal);
return await Postal.findAll({include: Communes}).then((communes:any)=>{
return communes;
}).catch((e:any)=>{console.error(e);return false;});
我不太了解文档,hasOne、belongsTo、belongsToMany 等方法之间的差异,不知道如何提出此关联请求。
请帮帮我
如果某个城市可以有多个邮政编码,同时某个邮政编码对于多个城市可以相同,那么城市和邮政编码之间存在 M:N 关系,您需要第三个路口table 喜欢 CommunesPostalCodes
:
const Communes = await this.db.define('commune', {
code_commune: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
libelle_commune: {
type: DataTypes.STRING,
allowNull: false
}
});
const Postal = await this.db.define('code_postal', {
code_postal: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
}
});
const CommunePostal = await this.db.define('commune_postal', {
id: {
type: DataTypes.SERIAL,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
code_commune: {
type: DataTypes.INTEGER,
allowNull: false
},
code_postal: {
type: DataTypes.INTEGER,
allowNull: false
}
});
// Define the relationship between the both tables
Postal.belongsToMany(Communes,{ through: CommunePostal, foreignKey: 'code_postal', otherKey: 'code_commune'});
Communes.belongsToMany(Postal,{ through: CommunePostal, foreignKey: 'code_commune', otherKey: 'code_postal'});
我不太明白 sequelize 关联是如何工作的。 我有一个包含两个表的 MariaDB 数据库:公社和 code_postals.
Database schema
code_commune 是 city_code 和 code_postal postal_code。所以,一个城市可以有多个postal_code,多个城市可以共享一个postal_code。
我的 SLQ 请求必须是这样的:
SELECT * FROM code_postals P INNER JOIN communes C ON P.code_commune=C.code_commune;
在我的 TypeScript 数据存储库中 class 我有这段代码:
const Communes = await this.db.define('commune', {
code_commune: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
libelle_commune: {
type: DataTypes.STRING,
allowNull: false
}
});
const Postal = await this.db.define('code_postal', {
code_commune: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
code_postal: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
}
});
// Define the relationship between the both tables
Postal.hasOne(Communes,{foreignKey: 'code_commune'});
Communes.belongsTo(Postal);
return await Postal.findAll({include: Communes}).then((communes:any)=>{
return communes;
}).catch((e:any)=>{console.error(e);return false;});
我不太了解文档,hasOne、belongsTo、belongsToMany 等方法之间的差异,不知道如何提出此关联请求。
请帮帮我
如果某个城市可以有多个邮政编码,同时某个邮政编码对于多个城市可以相同,那么城市和邮政编码之间存在 M:N 关系,您需要第三个路口table 喜欢 CommunesPostalCodes
:
const Communes = await this.db.define('commune', {
code_commune: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
libelle_commune: {
type: DataTypes.STRING,
allowNull: false
}
});
const Postal = await this.db.define('code_postal', {
code_postal: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
}
});
const CommunePostal = await this.db.define('commune_postal', {
id: {
type: DataTypes.SERIAL,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
code_commune: {
type: DataTypes.INTEGER,
allowNull: false
},
code_postal: {
type: DataTypes.INTEGER,
allowNull: false
}
});
// Define the relationship between the both tables
Postal.belongsToMany(Communes,{ through: CommunePostal, foreignKey: 'code_postal', otherKey: 'code_commune'});
Communes.belongsToMany(Postal,{ through: CommunePostal, foreignKey: 'code_commune', otherKey: 'code_postal'});