New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use @DefaultScope() simultaneously in two related Models #1703
Comments
[Error] TypeError: Cannot read properties of undefined (reading 'getTableName')
at Function._validateIncludedElement (C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:611:30)
at C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:542:37
at Array.map (<anonymous>)
at Function._validateIncludedElements (C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:537:39)
at Function._validateIncludedElement (C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:732:38)
at C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:542:37
at Array.map (<anonymous>)
at Function._validateIncludedElements (C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:537:39)
at Function.findAll (C:\Users\caiod\Desktop\campeonato-municipal\server\node_modules\sequelize\src\model.js:1794:12)
at processTicksAndRejections (node:internal/process/task_queues:95:5) |
Update: the issue seems to be related to recursive including one class onto another within the DefaultScopes, as i can workaround it by doing the following: import { ResponsavelType } from '@lib/types/responsavelType';
import { AllowNull, Column, Length, Table, DataType, Model, ForeignKey, BelongsTo, HasMany, Min, Max, PrimaryKey, Unique, Scopes, DefaultScope, Default, HasOne } from "sequelize-typescript";
import JogadorModel from './jogadorModel';
@DefaultScope(() => ({
include: [JogadorModel.unscoped()]
}))
@Table({
tableName: process.env.MODEL_RESPONSAVEL_TABLE_NAME,
paranoid: true,
})
export default class ResponsavelModel extends Model<ResponsavelType, Omit<ResponsavelType, "id">> {
@PrimaryKey
@Default(DataType.UUIDV4)
@Column(DataType.UUIDV4)
declare id: string;
@Unique
@AllowNull(false)
@Length({ min: 11, max: 11 })
@Column(DataType.STRING(11))
declare cpf: string;
@AllowNull(false)
@Length({ min: 1, max: 128 })
@Column(DataType.STRING(128))
declare nome_completo: string;
@AllowNull(false)
@Length({ min: 11, max: 13 })
@Column(DataType.STRING(13))
declare telefone: string;
@AllowNull(false)
@Length({ min: 1, max: 128 })
@Column(DataType.STRING(128))
declare email: string;
@ForeignKey(() => JogadorModel)
@Column(DataType.UUIDV4)
declare fk_jogador_id: string;
@BelongsTo(() => JogadorModel, {
onDelete: "CASCADE",
onUpdate: "CASCADE",
})
declare jogador: JogadorModel;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issue
Cannot use @DefaultScope() simultaneously in two related Models
Versions
Issue type
Actual behavior
I have two related models: Jogador and Responsavel, in which Jogador is the one referenced by Responsavel. I added a DefaultScope to Jogador, in which it retrieves all attributes and nested objects, including Responsavel; When it comes to Responsavel, i am trying to do the same: add a DefaultScope that retireves all attributes and the Jogador it is related to, but i cant do it because i keep getting the following error as soon as i add the DefaultScope to the ResponsavelModel (note the error only occurs once i add the DefaultScope to this one; if it is only in Jogador the error won't occur and the result will be as expected)
Expected behavior
It should let me query by default Jogador and retrieve it's Responsavel from database automatically; the same thing should happen with Responsavel, but in this case retrieving it's related Jogador.
Steps to reproduce
Start the sequelize-typescript instance and add DefaultScope to two related models
Related code
[sequelize-typescript instance]
[JogadorModel]
[ResponsavelModel]
The text was updated successfully, but these errors were encountered: