チート集

SQL直実行dataSource.query()配列が戻り値
カウントdataSource.getRepository(Trips).count({ where: { carId: carId } })条件付けないならcount()
where条件直記載dataSource.getRepository(PosRecords).count({where: {recorded: Raw((alias) => `${alias} between :from AND :to`,{from: '2022-10-1', to: '2022-11-1'})}});toとfromをパラメータ化

find/findBy

findはrelationなどいろいろ設定できるにたいし、findByは条件のみ。 Oneはその1レコードバージョンでAndCountだと件数も取れる

in条件In(array)

参考サイト

ManyToMany

https://zenn.dev/msksgm/articles/20211124-typeorm-many2many

自然に任せると勝手にテーブル推定してわけわからんので指定すること!StoreテーブルのPrefectureがこれ!

 @ManyToMany(() => Prefecture)
 @JoinTable({
     name: "store_prefecture", // table name for the junction table of this relation
     joinColumn: {
         name: "store_id",
         referencedColumnName: "id"
     },
     inverseJoinColumn: {
         name: "prefecture_id",
         referencedColumnName: "id"
     }
 })

トラブルシューティング

migrationテーブル

すでにDBがあるのにmigrationsテーブルがない(または消した)場合。

テーブルあるのに作ろうとしてエラー。

migrationでdatetimeのデフォルト値エラー

create table test (`created` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
create table test (`created` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
 @UpdateDateColumn({ name: 'modified', type: 'datetime', width: 6, precision: 0})

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS