Last active
October 6, 2023 18:47
-
-
Save shubham-sharmas/24644934133c10eee3a185f1ed9f9430 to your computer and use it in GitHub Desktop.
sequelize-issue-reproduction-11669
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Before running the program below, please ensure that you have set up Sequelize on your local system. | |
You can follow the instructions provided in this link: | |
https://github.com/sequelize/sequelize/blob/main/CONTRIBUTING.md#how-to-prepare-a-development-environment-for-sequelize | |
Alternatively, you can run the following commands to set up Sequelize: | |
git clone https://github.com/sequelize/sequelize.git | |
cd sequelize | |
yarn install | |
yarn start-postgres-latest | |
npm install -g typescript | |
npm install -g ts-node | |
// copy the file(11669.js) & paste in the current directory | |
DIALECT=postgres ts-node 11669.ts | |
*/ | |
import { DataTypes, Model } from "@sequelize/core"; | |
import { Attribute } from "@sequelize/core/decorators-legacy"; | |
import { createSequelizeInstance } from "./dev/sscce-helpers"; | |
class Complex extends Model { | |
@Attribute({ | |
autoIncrement: true, | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
primaryKey: true, | |
}) | |
declare id: number; | |
} | |
class Building extends Model { | |
@Attribute({ | |
autoIncrement: true, | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
primaryKey: true, | |
}) | |
declare id: number; | |
} | |
class AnotherEntity extends Model { | |
@Attribute({ | |
autoIncrement: true, | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
primaryKey: true, | |
}) | |
declare id: number; | |
} | |
class Unit extends Model { | |
@Attribute({ | |
autoIncrement: true, | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
primaryKey: true, | |
}) | |
declare id: number; | |
@Attribute({ | |
type: DataTypes.INTEGER, | |
field: "building_id", | |
allowNull: true, | |
references: { | |
model: Complex, | |
key: "id", | |
}, | |
}) | |
declare complex_id: number; | |
@Attribute({ | |
field: "immeuble_id", | |
type: DataTypes.INTEGER, | |
allowNull: true, | |
references: { | |
model: Building, | |
key: "id", | |
}, | |
}) | |
declare building_id: number; | |
@Attribute({ | |
type: DataTypes.INTEGER, | |
allowNull: true, | |
references: { | |
model: AnotherEntity, | |
key: "id", | |
}, | |
}) | |
declare another_entity_id: number; | |
} | |
const sequelize = createSequelizeInstance({ | |
benchmark: true, | |
models: [Complex, Building, AnotherEntity, Unit], | |
}); | |
const initAssociations = () => { | |
AnotherEntity.hasMany(Unit, { as: "units", foreignKey: "another_entity_id" }); | |
Unit.belongsTo(Building, { as: "building", foreignKey: "building_id" }); | |
Building.hasMany(Unit, { as: "units", foreignKey: "building_id" }); | |
Unit.belongsTo(Complex, { as: "complex", foreignKey: "complex_id" }); | |
Complex.hasMany(Unit, { as: "units", foreignKey: "complex_id" }); | |
Unit.belongsTo(AnotherEntity, { | |
as: "another_entity", | |
foreignKey: "another_entity_id", | |
}); | |
}; | |
(async () => { | |
await sequelize.sync({ force: true }); | |
initAssociations(); | |
await sequelize.authenticate(); | |
await Building.create(); | |
await Complex.create(); | |
await AnotherEntity.create(); | |
await Unit.create({ | |
complex_id: 1, | |
building_id: 1, | |
another_entity_id: 1, | |
}); | |
const unit = await Unit.findOne({ | |
where: { | |
complex_id: 1, | |
}, | |
}); | |
console.log('unit: ',unit); | |
const anotherEntity = await AnotherEntity.findOne({ | |
include: [ | |
{ | |
model: Unit, | |
as: "units", | |
where: { | |
complex_id: 1, | |
}, | |
}, | |
], | |
}); | |
console.log('anotherEntity: ',anotherEntity); | |
await sequelize.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment