{"record":{"id":"fe053546f53ea953","repo":"firefly-iii/firefly-iii","slug":"400004","errorCode":"400004","errorMessage":"400004: Could not store new currency.","messagePattern":"400004: Could not store new currency\\.","errorType":"exception","errorClass":"FireflyException","httpStatus":null,"severity":"error","filePath":"app/Factory/TransactionCurrencyFactory.php","lineNumber":70,"sourceCode":"            $old->forceDelete();\n            Log::warning(sprintf('Force deleted old currency with ID #%d and code \"%s\".', $old->id, $data['code']));\n        }\n\n        try {\n            /** @var TransactionCurrency $result */\n            $result = TransactionCurrency::create([\n                'name'           => $data['name'],\n                'code'           => $data['code'],\n                'symbol'         => $data['symbol'],\n                'decimal_places' => $data['decimal_places'],\n                'enabled'        => false,\n            ]);\n        } catch (QueryException $e) {\n            $result = null;\n            Log::error(sprintf('Could not create new currency: %s', $e->getMessage()));\n            Log::error($e->getTraceAsString());\n\n            throw new FireflyException('400004: Could not store new currency.', 0, $e);\n        }\n\n        return $result;\n    }\n\n    public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency\n    {\n        $currencyCode = e($currencyCode);\n        $currencyId   = (int) $currencyId;\n        $currency     = null;\n\n        if ('' === $currencyCode && 0 === $currencyId) {\n            Log::debug('Cannot find anything on empty currency code and empty currency ID!');\n\n            return null;\n        }\n\n        // first by ID:","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/firefly-iii/firefly-iii/blob/fd8791d08d4d9e6467519a78048cd038e26b8878/app/Factory/TransactionCurrencyFactory.php#L52-L88","documentation":"TransactionCurrencyFactory::create() inserts name, code, symbol, decimal_places (enabled forced to false) inside a try/catch. A QueryException is logged and rethrown as '400004: Could not store new currency.' — the SQL error is in the preceding log line. Note the factory does not check for an existing code first, so duplicates land here rather than in a friendly path.","triggerScenarios":"Creating a currency whose code (or name/symbol) already exists and hits a unique index; code/symbol/name exceeding column lengths; decimal_places outside the column's allowed range; a non-numeric decimal_places.","commonSituations":"Re-running setup or import routines that call create() unconditionally instead of find(); ISO 4217 codes colliding with existing rows; long currency names from third-party feeds.","solutions":["Check the logged 'Could not create new currency:' line for the SQL error.","Call find($currencyId, $currencyCode) before create() and only create when it returns null.","Truncate name/symbol to their column limits and cast decimal_places to int (0–12)."],"exampleFix":"// before\n$newCurrency = $factory->create(['name' => 'Euro', 'code' => 'EUR', 'symbol' => '€', 'decimal_places' => 2]);\n\n// after\n$currency = $factory->find(null, 'EUR') ?? $factory->create(['name' => 'Euro', 'code' => 'EUR', 'symbol' => '€', 'decimal_places' => 2]);","handlingStrategy":"validation","validationCode":"$existing = $factory->find(null, $data['code']);\nif (null !== $existing) {\n    return $existing; // never insert a duplicate code\n}\nif (strlen((string) $data['code']) > 255 || !is_numeric($data['decimal_places'] ?? null)) {\n    throw new InvalidArgumentException('Invalid currency payload');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $currency = $factory->create($data);\n} catch (FireflyException $e) {\n    $currency = $factory->find(null, $data['code']);\n    if (null === $currency) {\n        throw $e;\n    }\n}","preventionTips":["Always find-before-create on currency codes; they are unique.","Bound name/symbol lengths and cast decimal_places to int.","In importers, cache created currencies by code to avoid repeated inserts."],"tags":["firefly-iii","currency","database","query-exception","factory"],"backgroundTag":"database-insert-failed","analyzedSha":"fd8791d08d4d9e6467519a78048cd038e26b8878","analyzedAt":"2026-08-17T02:14:53.848Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}