orchid-orm 1.72.10 → 1.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -410,6 +410,13 @@ var BelongsToVirtualColumn = class extends VirtualColumn {
410
410
  this.nestedUpdate(queryForUpdate, set, data);
411
411
  }
412
412
  };
413
+ const getBelongsToRequired = (tableConfig, relation) => {
414
+ const { required } = relation.options;
415
+ if (typeof required === "boolean") return required;
416
+ return relation.options.columns.every((key) => {
417
+ return !tableConfig.columns.shape[key].data.isNullable;
418
+ });
419
+ };
413
420
  const makeBelongsToMethod = (tableConfig, table, relation, relationName, query) => {
414
421
  const primaryKeys = relation.options.references;
415
422
  const foreignKeys = relation.options.columns;
@@ -1408,7 +1415,7 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
1408
1415
  else if (type === "hasAndBelongsToMany") data = makeHasAndBelongsToManyMethod(table, dbTable, qb, relation, relationName, query, schema);
1409
1416
  else throw new Error(`Unknown relation type ${type}`);
1410
1417
  if (data.returns === "one") {
1411
- if (relation.options.required) _queryTake(query);
1418
+ if (type === "belongsTo" ? getBelongsToRequired(table, relation) : relation.options.required) _queryTake(query);
1412
1419
  else _queryTakeOptional(query);
1413
1420
  query.q.returnsOne = true;
1414
1421
  }