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.

Relational Query Builder giving error when adding to many-to-many relationship

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)

Steps to reproduce or a small repository showing the problem:

So I’ve been following the repository relations FAQ and other examples here, but it doesn’t seem to be working properly when trying to add an item to a many-to-many relationship

@Entity
class ContentItem
  @PrimaryGeneratedColumn('uuid')
  id: string;

 @ManyToMany(type => Image, image => image.contentItems)
  images: Image[]

 ...

@Entity()
export class Image {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ManyToMany(type => ContentItem, contentItem => contentItem.images)
  @JoinTable()
  contentItems: ContentItem[]

...

await getConnection()
        .createQueryBuilder()
        .relation(Image, "contentItems")
        .of(imageId)
        .add(contentItemId)

And the error response

code: "23502"
column: "image_id"
detail: "Failing row contains (null, null)."
file: "execMain.c"
length: 197
line: "2042"
message: "null value in column "image_id" violates not-null constraint"
name: "QueryFailedError"
parameters: []
query: "INSERT INTO "image_content_item"("image_id", "content_item_id") VALUES (DEFAULT, DEFAULT)"
routine: "ExecConstraints"
schema: "public"
severity: "ERROR"
stacktrace: ["QueryFailedError: null value in column "image_id" violates not-null constraint",…]
table: "image_content_item"

For some reason it’s not binding the parameters so the insert fails

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
codymartincommented, Jun 28, 2021

I have this exact issue too.

3reactions
tnwannacommented, Apr 16, 2019

I think it’s similar to #2612 and #3320

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query builder ManyToMany relationship - Stack Overflow
It doesn't work and give me the error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected. symfony ...
Read more >
Many-to-one / one-to-many relations - typeorm - GitBook
Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take...
Read more >
Object Relational Tutorial (1.x API)
A second directive, known as relationship() , tells the ORM that the Address class itself should be linked to the User class, using...
Read more >
Hibernate ORM 5.4.33.Final User Guide - Red Hat on GitHub
Querying for entities using entity relation joins; 21.17. ... toLongArray() ) { if ( builder.length() > 0 ) { builder.append( DELIMITER ); }...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
A one-to-one relationship is a very basic relation. ... Of course, since all relationships also serve as query builders, you can add further...
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