@@ -745,47 +745,76 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
745745 }
746746
747747 /**
748- * Sets a column to the value of another column with type-safe column matching.
748+ * Sets the values to update for an {@link Kysely.updateTable | update} query
749+ * using column references instead of values.
749750 *
750- * Unlike {@link set}, this method only allows you to reference columns that have
751- * the same type as the target column, ensuring type safety at compile time.
751+ * This method is similar to {@link set} but allows you to update columns by
752+ * referencing other columns instead of providing literal values. This is useful
753+ * when you want to copy values from one column to another or perform updates
754+ * based on existing column values.
755+ *
756+ * You can provide either two arguments (column name and reference) or a single
757+ * object where keys are column names and values are column references.
752758 *
753759 * ### Examples
754760 *
761+ * Update a column by referencing another column using the two-argument form:
762+ *
755763 * ```ts
756- * await db.updateTable('person')
757- * .setRef('first_name', 'last_name')
764+ * const result = await db
765+ * .updateTable('person')
766+ * .setRef('last_name', 'first_name')
758767 * .where('id', '=', 1)
759- * .execute ()
768+ * .executeTakeFirst ()
760769 * ```
761770 *
762771 * The generated SQL (PostgreSQL):
763772 *
764773 * ```sql
765- * update "person" set "first_name " = "last_name " where "id" = $1
774+ * update "person" set "last_name " = "first_name " where "id" = $1
766775 * ```
767776 *
768- * Type errors for mismatched types :
777+ * You can reference columns from joined tables in a PostgreSQL `from` query :
769778 *
770779 * ```ts
771- * await db.updateTable('person')
772- * .setRef('first_name', 'id') // Error: 'id' is number, 'first_name' is string
773- * .execute()
780+ * const result = await db
781+ * .updateTable('person')
782+ * .from('pet')
783+ * .setRef({
784+ * first_name: 'pet.name',
785+ * })
786+ * .whereRef('pet.owner_id', '=', 'person.id')
787+ * .executeTakeFirst()
788+ * ```
789+ *
790+ * The generated SQL (PostgreSQL):
791+ *
792+ * ```sql
793+ * update "person"
794+ * set "first_name" = "pet"."name"
795+ * from "pet"
796+ * where "pet"."owner_id" = "person"."id"
774797 * ```
775798 */
776799 setRef < RE extends ReferenceExpression < DB , UT > > (
777800 key : RE ,
778- value : MatchingReferenceExpression <
779- DB ,
780- UT ,
781- ExtractUpdateTypeFromReferenceExpression < DB , UT , RE >
782- > ,
801+ value : RE ,
802+ ) : UpdateQueryBuilder < DB , UT , TB , O >
803+
804+ setRef (
805+ updates : UpdateObjectWithRef < DB , TB , UT > ,
806+ ) : UpdateQueryBuilder < DB , UT , TB , O >
807+
808+ setRef (
809+ ...args :
810+ | [ ReferenceExpression < DB , UT > , ReferenceExpression < DB , UT > ]
811+ | [ UpdateObjectWithRef < DB , TB , UT > ]
783812 ) : UpdateQueryBuilder < DB , UT , TB , O > {
784813 return new UpdateQueryBuilder ( {
785814 ...this . #props,
786815 queryNode : UpdateQueryNode . cloneWithUpdates (
787816 this . #props. queryNode ,
788- parseUpdateWithRef ( key , value ) ,
817+ parseUpdateWithRef ( ... args ) ,
789818 ) ,
790819 } )
791820 }
0 commit comments