refactor(metadata): rename field_id to field_key in AssetMetadataValue

Rename the database column from field_id to field_key and the Prisma
property from fieldId to fieldKey to better reflect that the column
stores human-readable metadata field keys, not internal database IDs.

This fixes a bug where AI autofill wrote internal DB IDs instead of
field keys, causing metadata values to not display in the UI.

- Add SQL migration to rename the column
- Update metadata, search, and asset services
- Fix agent-autofill workflow to map fields by key
- Update all related test suites
This commit is contained in:
yiling
2026-05-28 12:38:07 +08:00
parent 5d5d631922
commit 89916a2950
15 changed files with 597 additions and 881 deletions
+1 -1
View File
@@ -1520,7 +1520,7 @@ export class AssetService {
} else if (mv.booleanValue !== null) {
value = mv.booleanValue
}
fieldValues.push({ fieldId: mv.fieldId, value })
fieldValues.push({ fieldId: mv.fieldKey, value })
}
}
+4 -4
View File
@@ -151,9 +151,9 @@ describe('MetadataService', () => {
where: { assetId: asset.id },
})
expect(values).toHaveLength(2)
const width = values.find((v) => v.fieldId === 'resolution_width')
const width = values.find((v) => v.fieldKey === 'resolution_width')
expect(width?.numberValue).toBe(1920)
const height = values.find((v) => v.fieldId === 'resolution_height')
const height = values.find((v) => v.fieldKey === 'resolution_height')
expect(height?.numberValue).toBe(1080)
})
@@ -196,9 +196,9 @@ describe('MetadataService', () => {
const val = await prisma.assetMetadataValue.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldId: {
assetId_fieldKey: {
assetId: asset.id,
fieldId: field.key,
fieldKey: field.key,
},
},
})
+3 -3
View File
@@ -309,14 +309,14 @@ export class MetadataService {
await tx.assetMetadataValue.upsert({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_fieldId: {
assetId_fieldKey: {
assetId: resolvedAssetId,
fieldId: req.key,
fieldKey: req.key,
},
},
create: {
assetId: resolvedAssetId,
fieldId: req.key,
fieldKey: req.key,
stringValue,
numberValue,
booleanValue,
+2 -2
View File
@@ -330,7 +330,7 @@ describe('ProjectService', () => {
await prisma.assetMetadataValue.create({
data: {
assetId: fileC.id,
fieldId: 'test-field',
fieldKey: 'test-field',
stringValue: 'test-value',
},
})
@@ -370,7 +370,7 @@ describe('ProjectService', () => {
expect(
await prisma.assetMetadataValue.findUnique({
// eslint-disable-next-line @typescript-eslint/naming-convention
where: { assetId_fieldId: { assetId: fileC.id, fieldId: 'test-field' } },
where: { assetId_fieldKey: { assetId: fileC.id, fieldKey: 'test-field' } },
}),
).toBeNull()
expect(await prisma.assetComment.findUnique({ where: { id: comment.id } })).toBeNull()
+1 -1
View File
@@ -81,7 +81,7 @@ describe('SearchService', () => {
await prisma.assetMetadataValue.create({
data: {
assetId: file2.id,
fieldId: 'status',
fieldKey: 'status',
stringValue: 'approved',
},
})
+5 -5
View File
@@ -235,14 +235,14 @@ export class SearchService {
}
// EAV queries on metadataValues
const baseFilter = { fieldId: field }
const baseFilter = { fieldKey: field }
// Handle Empty/NotEmpty globally for EAV
if (operator === 'isEmpty') {
return { metadataValues: { none: { fieldId: field } } }
return { metadataValues: { none: { fieldKey: field } } }
}
if (operator === 'isNotEmpty') {
return { metadataValues: { some: { fieldId: field } } }
return { metadataValues: { some: { fieldKey: field } } }
}
const valuePredicate = (): Prisma.AssetMetadataValueWhereInput => {
@@ -295,11 +295,11 @@ export class SearchService {
// "is not" in EAV context: either the field doesn't exist,
// OR it exists but has a different value.
OR: [
{ metadataValues: { none: { fieldId: field } } },
{ metadataValues: { none: { fieldKey: field } } },
{
metadataValues: {
some: {
fieldId: field,
fieldKey: field,
NOT: valuePredicate(),
},
},