Cannot update entity with relation

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x ] latest [ ] @next [ ] 0.x.x (or put your version here)

Example

Given these entities:

@Entity('users')
export default class UserEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;
    
  @Column()
  name: string

  @OneToMany(type => Photo, photo => photo.user, { eager: true })
  photos: Photo[];
}


@Entity('photos')
export class Photo {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  url: string;

  @ManyToOne(type => User, user => user.photos)
  user: User;
}

Given these update and create functions:


export async function createUser(user): Promise<UserEntity> {
  const userRepo: Repository<UserEntity> = getRepository(UserEntity);

  const newUser = userRepo.create(user);
  return userRepo.save(newUser)
}

export async function updateUser(existingUser: UserEntity) {
  const userRepo: Repository<UserEntity> = getRepository(UserEntity);
	
  return userRepo.update({ id: existingUser.id }, existingUser)
}

Updating fails:

const user =  await createUser({ name: 'foo'})
user.name = 'bar'
await updateUser(user)

With this error: EntityColumnNotFound: No entity column "photos" was found.

Stacktrace:

EntityColumnNotFound: No entity column "photos" was found.
    at new EntityColumnNotFound (/home/olingern/work/server/node_modules/typeorm/error/EntityColumnNotFound.js:10:28)
    at /home/olingern/work/server/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:318:27
    at Array.forEach (<anonymous>)
    at UpdateQueryBuilder.createUpdateExpression (/home/olingern/work/server/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:314:85)
    at UpdateQueryBuilder.getQuery (/home/olingern/work/server/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:40:24)
    at UpdateQueryBuilder.QueryBuilder.getQueryAndParameters (/home/olingern/work/server/node_modules/typeorm/query-builder/QueryBuilder.js:224:26)
    at UpdateQueryBuilder.<anonymous> (/home/olingern/work/server/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:81:50)
    at step (/home/olingern/work/server/node_modules/tslib/tslib.js:136:27)
    at Object.next (/home/olingern/work/server/node_modules/tslib/tslib.js:117:57)
    at /home/olingern/work/server/node_modules/tslib/tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/home/olingern/work/server/node_modules/tslib/tslib.js:106:16)
    at UpdateQueryBuilder.execute (/home/olingern/work/server/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:49:24)
    at EntityManager.update (/home/olingern/work/server/node_modules/typeorm/entity-manager/EntityManager.js:300:18)
    at Repository.update (/home/olingern/work/server/node_modules/typeorm/repository/Repository.js:102:29)
    at Object.updateUser (/home/olingern/work/server/dist/app/services/UserService.js:55:21)
    at getOrganisationSettings (/home/olingern/work/server/dist/app/controllers/UserController.js:253:27)
    at processTicksAndRejections (internal/process/task_queues.js:89:5) {
  name: 'EntityColumnNotFound',
  message: 'No entity column "photos" was found.'
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:30
  • Comments:20

github_iconTop GitHub Comments

28reactions
TidianeRollandcommented, Jan 28, 2020

Euh yes. This is my code

async update(newuser: User) {
       // newUser and My old user have the same matricule
       const getUserFromDB = await this.userRepository.findOne({matricule: newuser.matricule });
       this.userRepository.merge(getUserFromDB, newuser);
       this.userRepository.save(getUserFromDB)
          .then(
                      (data) => {
                          console.log('success');
                      },
                      (err) => {
                          // error
                          console.log(err);
                      },
                  );
    }

After adding

@ManyToOne(type => User, user=> user.photo, 
{onUpdate: 'CASCADE', onDelete: 'CASCADE'}
)

I have no more the error : EntityColumnNotFound: No entity column “photos” was found. Hope it helps

24reactions
sippeangelocommented, May 5, 2020

So how are you actually supposed update a relation without fetching the whole entity with find, merge and save? This isn’t atomic in any way. I don’t want to update the whole entity data, I just want to add a row to the id-to-id table that describes the relation…

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - I can't update/edit One to one relationship with Entity ...
This is how you can update all the tables public void Edit(Student student) { var existingStudent = _context.Students.FirstOrDefault(s => s.
Read more >
Managing One To Many Relationships With Entity Framework ...
This first section explores a number of ways in which relationships can be created between an existing principal entity and newly created dependents....
Read more >
Tutorial: Update related data with EF in an ASP.NET MVC app
In this tutorial you'll update related data. For most relationships, this can be done by updating either foreign key fields or navigation ...
Read more >
Modifying Data with Entity Famework Core - Code Maze
The SaveChanges method ignores it; Added – The entity doesn't exist in the database ... Let's see how to update the relationship in...
Read more >
Hibernate Envers ValidityAuditStrategy throws a "Cannot ...
If no row exists with this requirement when an update to an entity occurs, this strategy will throw this exception because the prior...
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