-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
HasOne relation with Null Value #1049
Comments
Hi, I'm a colleague of @sergefabre, I'll try to add some details about our problem. We're using the preload method on a query builder (Signalement). let sql = Signalement.query().preload('anomalie', (ano) =>
ano.select(['id', 'statut', 'commentaireCloture', 'timestampDerniereModif'])
) Here is the queryBuilder code for hasOne relationship: And here the queryBuilder code for belongsTo relationship: It is clear that in the hasOne relationship, a value equal to null or undefined throws an exception, in the getValue() method as it uses ensureValue method: export function ensureValue(collection: any, key: string, missingCallback: () => void) {
const value = collection[key]
if (value === undefined || value === null) {
missingCallback()
return
}
return value
} Whereas the belongsTo relationship only checks for undefined values and allows null values. const foreignKeyValues = this.parent
.map((model) => model[this.relation.foreignKey])
.filter((foreignKeyValue) => {
if (foreignKeyValue === undefined) {
this.raiseMissingForeignKey()
}
return foreignKeyValue !== null
}) This is not what is mentioned in the docs : https://v5-docs.adonisjs.com/guides/models/relationships#preload-relationship We found the related question in SO, applied the answer (using belongsTo relationship instead of hasOne) and now it works. @thetutlage what are your thoughts on this ? Is this by design ? Perhaps the docs should mention this explicitly. Kind regards |
Can share the relationships and the table schema for these relationships |
Model Signalement @belongsTo(() => Anomalie, {
foreignKey: 'anomalieId',
localKey: 'id',
onQuery: (ano) => {
if (ano.client.dialect.name === 'postgres') ano.withSchema(Anomalie.schema)
},
})
public anomalie: BelongsTo<typeof Anomalie>` Model Anomalie : @column({ isPrimary: true }) public id: number
@column.dateTime({ autoCreate: true }) public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true }) public updatedAt: DateTime
@column() public statut: string
@column() public commentaireCloture: string
@column.dateTime() public timestampDerniereModif: DateTime
@hasMany(() => Signalement, {
localKey: 'id',
foreignKey: 'anomalieId',
})
public signalements: HasMany<typeof Signalement> |
anomalie_id of table "Signalements", is "NULLABLE" |
hi guy, I had the same problem as you |
hi `Vine.macro('defaultSetNull', addNullableToAny) /**
interface VineString { interface VineDate { |
Package version
18.4.0
Describe the bug
the Doc say :
but, with hasone, the relation not work with null value. It's OK with relation BelongTo
Reproduction repo
https://github.com/adonisjs/lucid/blob/v18.4.0/src/Orm/Relations/HasOne/QueryBuilder.ts#L84
The text was updated successfully, but these errors were encountered: