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.

Using plain properties (not columns) in entity classes

See original GitHub issue

Hello, my issue would be the same as https://github.com/typeorm/typeorm/issues/3569

@Entity()
export class User extends Base{

    @Column('varchar', { length: 255, unique: true })
    @IsEmail({}, { message: "Invalid email." })
    @IsUnique()
    email: string;

    @Column('varchar', { length: 255 })
    @MinLength(6, { message: "Password must have at least 6 characters" })
    password: string;

    @Column('varchar', { length: 255 })
    salt: string;

    @Column('varchar', { length: 255 })
    role: string;

    password_changed: number = 0;

    @BeforeUpdate()
    setDataBeforeUpdate(){
        if (this.password_changed){
            this.password = PasswordHelper.hashString(this.password, this.salt);
        }
    }
}

Let’s take the above piece of code… i want the password_changed field to not be a column…just a plain old property for this class. The problem is that on update

const repo: Repository<User> = getRepository(User);
await repo.update(row.id, row).then().catch( (e) => { throw e; });

i get this error

EntityColumnNotFound: No entity column "password_changed" was found.
    at new EntityColumnNotFound (.../src/error/EntityColumnNotFound.ts:8:9)
    at .../src/query-builder/UpdateQueryBuilder.ts:403:27
    at Array.forEach (<anonymous>)
    at UpdateQueryBuilder.createUpdateExpression (.../src/query-builder/UpdateQueryBuilder.ts:398:68)
    at UpdateQueryBuilder.getQuery (.../src/query-builder/UpdateQueryBuilder.ts:50:24)

Thanks.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:9

github_iconTop GitHub Comments

7reactions
cstapetreicommented, Sep 23, 2020

hey @cstapetrei, i have the same problem as yours, i fixed it by deleting the property before an update. In your case

const repo: Repository<User> = getRepository(User);
delete row. password_changed;
await repo.update(row.id, row).then().catch( (e) => { throw e; });

Hehe…nice hack 😄

I’ll give it a go…buuuut it would be nicer to be fixed … even with a @NotColumn() decorator or smth like that 😄

3reactions
paulopradocommented, May 26, 2021

Any updates? Deleting the field is a nice hack, but it may affect the code once the property will no longer exists in the object.

The idea @cstapetrei of @NotCollumn() decorator seems pretty good

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 2. Mapping Entities - Red Hat on GitHub
The class Flight is mapped to the Flight table, using the column id as its primary ... In plain Java APIs, the temporal...
Read more >
DataAnnotations - NotMapped Attribute in EF 6 & EF Core
The NotMapped attribute can be applied to properties of an entity class for which we do not want to create corresponding columns in...
Read more >
java - JPA Query selecting only specific columns without using ...
Another way is to wrap the selected properties in a custom object and execute it in a TypedQuery: String query = "SELECT NEW...
Read more >
Best Practices and Common Pitfalls of Using JPA (Hibernate ...
1. The entity class must have a no-arg constructor. The entity class may have other constructors as well. The no-arg constructor must be...
Read more >
Computed Properties and Entity Framework - Dave Glick
These properties work fine once you've obtained the entities and want to work with them. The problems start when you want to use...
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