TypeORM one-to-many / many-to-one array empty and foreign key is null

Viewed 653

I must be doing something wrong and I can not solve this issue.

I have two entities in my API

  • Collection
  • Asset

Each asset belongs to one collection A Collection holds many Assets

These are my entity classes

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  JoinColumn,
  OneToOne,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';
import { CollectionStats } from '../../../database/entities/opensea/CollectionStats';
import { Asset } from './Asset';
import { AssetContract } from './interfaces/AssetContract';
import { PaymentToken } from './interfaces/PaymentToken';

@Entity()
export class Collection {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({ nullable: true })
  external_link: string;

  @Column({ nullable: true })
  description: string;

  @Column({ unique: true, nullable: false })
  slug: string;

  @Column({ nullable: true })
  image_url: string;

  @Column({ nullable: true })
  banner_image_url: string;

  @Column()
  dev_seller_fee_basis_points: string;

  @Column()
  safelist_request_status: string;

  @Column({ nullable: true })
  payout_address: string;

  @Column('jsonb')
  primary_asset_contracts: AssetContract[];

  @Column('simple-json')
  traits: object;

  @Column('jsonb', { nullable: true })
  payment_tokens: PaymentToken[];

  @Column('varchar', { array: true, nullable: true })
  editors: string[];

  @OneToOne(() => CollectionStats, { eager: true, cascade: true })
  @JoinColumn()
  stats: CollectionStats;

  @OneToMany(() => Asset, (asset) => asset.collection, { cascade:true, eager: true })
  assets: Asset[];

  @Column({ type: 'timestamptz' })
  created_date: Date;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}

import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { ColumnNumericOptions } from 'typeorm/decorator/options/ColumnNumericOptions';
import { Collection } from './Collection';
import { CollectionStats } from './CollectionStats';
import { AssetContract } from './interfaces/AssetContract';
import { Owner } from './interfaces/Owner';
import { PaymentToken } from './interfaces/PaymentToken';
import { Trait } from './interfaces/Trait';

@Entity()
export class Asset {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: false })
  token_id: string;

  @Column({ default: 0 })
  num_sales: number;

  @Column({ nullable: true })
  background_color: string;

  @Column({ nullable: true })
  image_url: string;

  @Column({ nullable: true })
  image_preview_url: string;

  @Column({ nullable: true })
  image_thumbnail_url: string;

  @Column({ nullable: true })
  image_original_url: string;

  @Column({ nullable: true })
  animation_original_url: string;

  @Column({ nullable: true })
  external_link: string;

  @Column({ nullable: true })
  description: string;

  @Column('simple-json')
  asset_contract: AssetContract;

  @Column({ nullable: true })
  permalink: string;

  @ManyToOne(() => Collection, (collection) => collection.assets)
  collection: Collection;

  @Column({ nullable: true })
  decimals: number;

  @Column({ nullable: true })
  token_metadata: string;

  @Column('simple-json')
  owner: Owner;

  @Column('simple-json', { array: false, nullable: true })
  sell_orders: Object;

  @Column('simple-json', { nullable: true })
  traits: Trait[];

  @Column('simple-json', { nullable: true })
  last_sale: Object;

  @Column('simple-json', { nullable: true })
  top_bid: Object;

  @Column('timestamptz', { nullable: true })
  listing_date: Date;

  @Column({ default: false })
  is_presale: boolean;

  @Column('simple-json', { nullable: true })
  transfer_fee_payment_token: PaymentToken;

  @Column({ nullable: true })
  transfer_fee: number;
}

This is the the function where everything should get linked together

 const collection = await getCustomRepository(CollectionRepository).create(
        collectionResponse.collection as Object,
      );

      const assets = await this.fetchAssets(collection); //Returns an Array of type Asset

      const stats = await getCustomRepository(CollectionStatsRepository).create(
        statsResponse.stats as Object,
      );

      collection.stats = stats; //this works properly
      collection.assets = assets; //this is not working

      return await getCustomRepository(CollectionRepository).save(collection);

The response from getCustomRepo(CollectionRepo).save(collection) actually shows all the collection data including an array holding all the assets as I want it to be. However, when I open the postgres client, the collection table has no "assets" column and the assets table's collectionId column only contains "null"

the collection.find() also is only returning an empty assets[]

What am I doing wrong here?

0 Answers
Related