0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TypeORMと実際のデータベース構造の不一致:EntityMetadataNotFoundエラーの解決方法

Posted at

:frowning2:
TypeORMに関する問題:EntityMetadataNotFound: No metadata for "" was found*

皆さん、こんにちは!最近、TypeORMを使用してプロジェクトを進める中で、特定のエンティティに関連する問題に直面しました。私はこの問題の解決方法を共有したいと思いますので、同じ問題に直面している方の参考になればと思います。

問題の詳細

以下は、問題が発生する前のエンティティクラスの一部です:

import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from "typeorm";
import { PostBodyType } from "../constants";

@Entity('content_posts')
export class PostEntity extends BaseEntity {
    @PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
    id: string;
    // ... その他のフィールド
}

解決方法

私が行った主な変更は、PrimaryColumntypeおよびgeneratedのプロパティです。varcharからuuidへの変更と、generatedプロパティの変更です。

以下は、修正後のエンティティクラスの一部です:

import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from "typeorm";
import { PostBodyType } from "../constants";

@Entity('content_posts')
export class PostEntity extends BaseEntity {
    @PrimaryColumn({ type: 'uuid', generated: 'uuid' })
    id: string;
    // ... その他のフィールド
}

関連するデータベースのテーブル構造は以下のとおりです:

CREATE TABLE content_posts (
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    title varchar NOT NULL,
    body varchar NOT NULL,
    summary varchar NULL,
    keywords _text NULL,
    "type" varchar NOT NULL DEFAULT 'MD'::character varying,
    "publishedAt" timestamptz NULL,
    "customOrder" int4 NOT NULL DEFAULT 0,
    "createdAt" timestamp NOT NULL DEFAULT now(),
    "updatedAt" timestamp NOT NULL DEFAULT now(),
    CONSTRAINT "PK_1234567890abcdef" PRIMARY KEY (id)
);

結論

@Entityが原因でTypeORMのEntityMetadataNotFound: No metadata for "*" was foundのような問題に遭遇した場合、最初にエンティティクラスとデータベース構造が一致しているかを確認してください。これが最も一般的な原因の一つと考えられます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?