Repository: directus/directus
File: api/src/services/fields.ts (TypeScript)

Code before:
				const columnToCompare =
					opts?.bypassLimits && opts.autoPurgeSystemCache === false ? sanitizeColumn(existingColumn) : existingColumn;

				if (!isEqual(columnToCompare, hookAdjustedField.schema)) {
					try {
						await transaction(this.knex, async (trx) => {
							await trx.schema.alterTable(collection, async (table) => {
								if (!hookAdjustedField.schema) return;
								this.addColumnToTable(table, collection, field, existingColumn);
							});
						});
					} catch (err: any) {
						throw await translateDatabaseError(err);
					}
				}
			}

			// Only create/update a database record if this is not a system field
			if (hookAdjustedField.meta && !isSystemField(collection, hookAdjustedField.field)) {
				if (record) {
					await this.itemsService.updateOne(
						record.id,
						{
							...hookAdjustedField.meta,
							collection: collection,
							field: hookAdjustedField.field,
						},
						{ emitEvents: false },
					);
				} else {
					await this.itemsService.createOne(
						{
							...hookAdjustedField.meta,
							collection: collection,
							field: hookAdjustedField.field,
						},
						{ emitEvents: false },
					);
				}
			}

			const actionEvent = {
				event: 'fields.update',
				meta: {
					payload: hookAdjustedField,
					keys: [hookAdjustedField.field],
					collection: collection,
				},
				context: {
					database: getDatabase(),
					schema: this.schema,

Review comment (bug):
I'd say for once, the comment is very redundant and maybe it makes more sense to throw an error here instead as right now, while the updating gets skipped, we're still emitting an action event even though nothing changed.

Code after:
				if (existingColumn.is_primary_key) {
					if (hookAdjustedField.schema?.is_nullable === true) {
						throw new InvalidPayloadError({ reason: 'Primary key cannot be null' });
					}
				}

				// Sanitize column only when applying snapshot diff as opts is only passed from /utils/apply-diff.ts
				const columnToCompare =
					opts?.bypassLimits && opts.autoPurgeSystemCache === false ? sanitizeColumn(existingColumn) : existingColumn;

				if (!isEqual(columnToCompare, hookAdjustedField.schema)) {
					try {
						await transaction(this.knex, async (trx) => {
							await trx.schema.alterTable(collection, async (table) => {
								if (!hookAdjustedField.schema) return;
								this.addColumnToTable(table, collection, field, existingColumn);
							});
						});
					} catch (err: any) {
						throw await translateDatabaseError(err, field);
					}
				}
			}

			// Only create/update a database record if this is not a system field
			if (hookAdjustedField.meta && !isSystemField(collection, hookAdjustedField.field)) {
				if (record) {
					await this.itemsService.updateOne(
						record.id,
						{
							...hookAdjustedField.meta,
							collection: collection,
							field: hookAdjustedField.field,
						},
						{ emitEvents: false },
					);
				} else {
					await this.itemsService.createOne(
						{
							...hookAdjustedField.meta,
							collection: collection,
							field: hookAdjustedField.field,
						},
						{ emitEvents: false },
					);
				}
			}

			const actionEvent = {
				event: 'fields.update',
				meta: {