Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
moheldesoqy opened this issue Apr 22, 2024 · 1 comment

Comments

@moheldesoqy
Copy link

moheldesoqy commented Apr 22, 2024

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"

@moheldesoqy moheldesoqy changed the title Custom attribute i added is not updating in the database when sending a request? Custom attribute after extending entity is not updating in the database when sending a request? Apr 22, 2024
@ByronKweh
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants