MCPcopy
hub / github.com/directus/directus / updateOne

Method updateOne

api/src/services/relations.ts:314–434  ·  view source on GitHub ↗

* Update an existing foreign key constraint * * Note: You can update anything under meta, but only the `on_delete` trigger under schema

(
		collection: string,
		field: string,
		relation: Partial<Relation>,
		opts?: MutationOptions,
	)

Source from the content-addressed store, hash-verified

312 * Note: You can update anything under meta, but only the `on_delete` trigger under schema
313 */
314 async updateOne(
315 collection: string,
316 field: string,
317 relation: Partial<Relation>,
318 opts?: MutationOptions,
319 ): Promise<void> {
320 if (this.accountability && this.accountability.admin !== true) {
321 throw new ForbiddenError();
322 }
323
324 const collectionSchema = this.schema.collections[collection];
325
326 if (!collectionSchema) {
327 throw new InvalidPayloadError({ reason: `Collection "${collection}" doesn't exist` });
328 }
329
330 const fieldSchema = collectionSchema.fields[field];
331
332 if (!fieldSchema) {
333 throw new InvalidPayloadError({ reason: `Field "${field}" doesn't exist in collection "${collection}"` });
334 }
335
336 const existingRelation = this.schema.relations.find(
337 (existingRelation) => existingRelation.collection === collection && existingRelation.field === field,
338 );
339
340 if (!existingRelation) {
341 throw new InvalidPayloadError({
342 reason: `Field "${field}" in collection "${collection}" doesn't have a relationship.`,
343 });
344 }
345
346 const runPostColumnChange = await this.helpers.schema.preColumnChange();
347 this.helpers.schema.preRelationChange(relation);
348
349 const nestedActionEvents: ActionEventParams[] = [];
350
351 try {
352 await transaction(this.knex, async (trx) => {
353 if (existingRelation.related_collection) {
354 await trx.schema.alterTable(collection, async (table) => {
355 let constraintName: string = getDefaultIndexName('foreign', collection, field);
356
357 // If the FK already exists in the DB, drop it first
358 if (existingRelation?.schema) {
359 constraintName = existingRelation.schema.constraint_name || constraintName;
360 table.dropForeign(field, constraintName);
361
362 constraintName = this.helpers.schema.constraintName(constraintName);
363 existingRelation.schema.constraint_name = constraintName;
364 }
365
366 this.alterType(table, relation, fieldSchema.nullable);
367
368 const builder = table
369 .foreign(field, constraintName || undefined)
370 .references(
371 `${existingRelation.related_collection!}.${

Callers 3

applyDiffFunction · 0.95
handlerFunction · 0.95
resolveSystemAdminFunction · 0.95

Calls 13

alterTypeMethod · 0.95
updateOneMethod · 0.95
createOneMethod · 0.95
transactionFunction · 0.85
getDefaultIndexNameFunction · 0.85
clearSystemCacheFunction · 0.85
getSchemaFunction · 0.85
preColumnChangeMethod · 0.80
preRelationChangeMethod · 0.80
constraintNameMethod · 0.80
onUpdateMethod · 0.80
postColumnChangeMethod · 0.80

Tested by

no test coverage detected