v2.5.2
Giriş yap

Sequelize "AssociationError [SequelizeAssociationError]: You have used the alias CustomsExitUpAd in two separate associations. Aliased associations must have unique aliases." Hatası alıyorum

melikekozan
358 defa görüntülendi

Aşağıdaki gibi yazdığım bir apim var. iki modeli birbirine bağlıyorum. Her şey istediğim gibi çalışıyor. Fakat birden fazla sayfalarda apiyi çalıştırınca hata alıyorum. As ile verdiğim isimlendirmeye kızıyor ve uniq olması için uyarı veriyor. Nasıl çözebileceğim hakkında yardım edebilir misiniz?

        InstallationInstructions.belongsTo(Users, {foreignKey: 'user_id'});
        InstallationInstructions.belongsTo(Interlocutors, {foreignKey: 'customer_id'});
        InstallationInstructions.belongsTo(Drivers, {foreignKey: 'driver_id'});
        InstallationInstructions.belongsTo(Customs, {as: "CustomsExitUpAd",foreignKey: 'customs_of_exit_id'});
        InstallationInstructions.belongsTo(Customs, {as:"CustomsGateUpAd", foreignKey: 'exit_customs_gate_id'});
        InstallationInstructions.hasMany(InstallationInstructionPlates, {foreignKey: 'upload_instruction_id'});
        InstallationInstructions.hasMany(InstallationInstructionsUploadAddress, {foreignKey: "installation_instruction_id"})
        InstallationInstructionPlates.belongsTo(Vehicles, {foreignKey: 'license_plate_id'});
        Interlocutors.hasMany(InterlocutorContact, {foreignKey: 'interlocutor_id'});
        InstallationInstructions.hasMany(SenderBuyer, {foreignKey: "installation_instruction_id"})
        if (req.method === "GET") {
            const {id} = req.query;
            const agreement = await InstallationInstructions.findOne({
                where: {
                    id: id
                },
                include: [
                    {
                        model: Interlocutors,
                    },
                    {
                        model: Users,
                    },
                    {
                        model: Drivers,
                    },
                    {
                        model: Customs,
                        as: "CustomsExitUpAd",
                    },
                    {
                        model: Customs,
                        as: "CustomsGateUpAd",
                    },
                    {
                        model: InstallationInstructionPlates
                    },
                    {
                        model: InstallationInstructionsUploadAddress
                    },
                    {
                        model: InstallationInstructionPlates,
                        include:[{
                            model: Vehicles
                        }],
                    },
                    {
                        model: Interlocutors,
                        include:[{
                            model: InterlocutorContact
                        }],
                    },
                    {
                        model: SenderBuyer
                    }
                ],
            })
    
Cevap yaz
Cevaplar (1)
f4kor4ll
397 gün önce

Hatanın sebebi, InstallationInstructions modelinin Customs modeli ile iki kez ilişkilendirilmesi ve her ilişkilendirme için aynı isimlendirme alias'ı CustomsExitUpAd kullanılmasıdır. Bu durum, iki kez ilişkilendirilen modellerin ayırt edilmesini zorlaştırdığı için Sequelize tarafından hata olarak algılanmaktadır.

Bu hatayı düzeltmek için, her ilişkilendirme için farklı bir alias ismi kullanmanız gerekiyor. Örneğin:

InstallationInstructions.belongsTo(Customs, {as: "CustomsExitUpAd", foreignKey: 'customs_of_exit_id'});
InstallationInstructions.belongsTo(Customs, {as:"CustomsGateUpAd", foreignKey: 'exit_customs_gate_id'});

yerine

InstallationInstructions.belongsTo(Customs, {as: "CustomsExitUpAd", foreignKey: 'customs_of_exit_id'});
InstallationInstructions.belongsTo(Customs, {as:"CustomsGateUpAd2", foreignKey: 'exit_customs_gate_id'});

gibi farklı bir alias kullanarak aynı modeli iki kez ilişkilendirebilirsiniz.

Daha fazla bilgi için Sequelize belgelerindeki (https://sequelize.org/docs/v6/core-concepts/assocs/#aliases) Associations - Aliases bölümüne bakabilirsiniz.