question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Entity lacks primary column properties in afterRemove() subscription.

See original GitHub issue

Issue type: [x] bug report

Database system/driver: [x] postgres

TypeORM version: [x] latest

Steps to reproduce or a small repository showing the problem:

Say we have User entity with login as primary column and Proposal entity with id primary column.

Then we define the following Rating entity as a join table for those ones:

@Entity()
export class Rating {

    @ManyToOne(_type => User,     { primary: true, onDelete: 'CASCADE' })
    @JoinColumn({ name: 'raterLogin' })
    rater!: Promise<User>;

    @ManyToOne(_type => Proposal, { primary: true, onDelete: 'CASCADE' })
    @JoinColumn({ name: 'proposalId' })
    proposal!: Promise<Proposal>;
    
    @PrimaryColumn()    
    raterLogin!: string;

    @PrimaryColumn()
    proposalId!: number;

    @Column()
    liked!: boolean;
}

When we define a subscriber for Rating and call ratingRepo.remove(ratingEntity), then afterRemove() method gets called with ratingEntity as one of the arguments. But why do its primary column properties raterLogin and proposalId have value undefined? You may check it in the debugger local variables window:

image

Why do we need to assemble the entity by parts and get primary columns from e.entityId? Why did you define TypeScript type for this event to be full-fledged Entity | undefined instead of Partial<Entity> | undefined if you decided to remove primary columns?

Other methods (afterInsert() and afterUpdate()) get entities with live primary columns in place.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
jmezzeragpcommented, Jun 16, 2021

I’m facing the same issue and found out that RemoveEvent<Entity> has an entityId field

1reaction
jaypcommented, Mar 19, 2020

The solution I used was to capture the value before delete and then put it back in the record.

Thanks. Superhacky but I did the same. I created a shadow variable to store the primary key. lol.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decorator reference - typeorm - GitBook
@PrimaryColumn. Marks a property in your entity as a table primary column. Same as @Column decorator but sets its primary option to true....
Read more >
Entity Framework: table without primary key - Stack Overflow
Some tables do not have primary keys defined so that when I create a new Entity Data Model, I get the following message:...
Read more >
Magento 2 EAV Model - Things You May Not Know
Magento EAV works based on the three main tables: Entity table; Attribute table; Value table. In case you want to add more attributes,...
Read more >
oracle.jbo.server Class ViewObjectImpl
If Applications override buildRangePaging() method to create a ... Constructs an array of entity attribute definitions of the primary keys of the View ......
Read more >
Class Table | CakePHP 4.1
The primary way to retrieve data is using Table::find(). ... an entity or throw a PersistenceFailedException if the entity is new, has no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found