medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.32k stars 2.38k forks source link

Custom attribute after extending entity is not updating in the database when sending a request? #7120

Open moheldesoqy opened 4 months ago

moheldesoqy commented 4 months ago

Hello!

So i extended the entiy of products, to add a value called "views", this is what i've done:

product model:

import { Column, Entity } from "typeorm"
import {
  // alias the core entity to not cause a naming conflict
  Product as MedusaProduct,
} from "@medusajs/medusa"

@Entity()
export class Product extends MedusaProduct {
  @Column()
  views: number
}

index.d.ts because i am using typescript:

export declare module "@medusajs/medusa/dist/models/product" {
  declare interface Product {
    views: number;
  }
}

migration script:

import { MigrationInterface, QueryRunner } from "typeorm";

export class Views1713779572506 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            "ALTER TABLE \"product\"" +
            " ADD COLUMN \"views\" integer DEFAULT 0"
        )
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            "ALTER TABLE \"product\" DROP COLUMN \"views\""
        )
    }
}

my index.ts code in src/api/index.ts:

import { registerOverriddenValidators } from "@medusajs/medusa"
import {
   AdminPostProductsReq as MedusaAdminPostProductsReq,
} from "@medusajs/medusa/dist/api/routes/admin/products/create-product"
import { IsString, IsNumber, IsOptional } from "class-validator"

class AdminPostProductsReq extends MedusaAdminPostProductsReq {
   @IsNumber()
   views: number
}

registerOverriddenValidators(AdminPostProductsReq)

I am sending a create product request with title, and the views value and its creating the product successfully, however i look at my products table in postgres and its still 0, the default value

help in why its not updating would be appreciated 🙏

note: the product is being added however the logs are saying "error: An error occurred while processing product.created: QueryFailedError: null value in column "title" of relation "product" violates not-null constraint"

ByronKweh commented 4 months ago

Could you send the payload and maybe the staging URL you have that I could test against?