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

Composite primary key consisting of composite primary key #1079

Closed
dystopiandev opened this issue Nov 15, 2020 · 4 comments
Closed

Composite primary key consisting of composite primary key #1079

dystopiandev opened this issue Nov 15, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@dystopiandev
Copy link

Describe the bug
When I call em.persistAndFlush on an entity whose composite primary key consists of other composite keys, there seems to be issues with the "nesting" in the resultant query.

Stack trace
Logged query:

insert into "Deposit" ("amount", "createdAt", "gatewayKey", "status", "txRef", "updatedAt", "wallet_currencyRef") values ('98765', '2020-11-15T09:46:45.128Z', 'STATUS', 'UNPAID', 'b7b838d5-cc87-4599-aad4-476bcde7d02e', '2020-11-15T09:46:45.128Z', 'USD,1')

To Reproduce

em.transactional(async (em) => {
  const wallet = await em.findOne(Wallet, {
    currencyRef: "USD",
    owner: { _id: 1 }
  })

  if (wallet) {
    em.persistAndFlush(
      em.create(Deposit, {
        wallet,
        depositGatewayKey: "STRIPE",
        amount: "98765",
        txRef: UUIDv4()
      })
    )
  }
})

Expected behavior
The correct query should be:

insert into "Deposit" ("amount", "createdAt", "gatewayKey", "status", "txRef", "updatedAt", "wallet_currencyRef", "wallet_owner") values ('98765', '2020-11-15T09:46:45.128Z', 'STATUS', 'UNPAID', 'b7b838d5-cc87-4599-aad4-476bcde7d02e', '2020-11-15T09:46:45.128Z', 'USD', '1')

Additional context
Entities:

@Entity()
class User {
  @PrimaryKey({ type: BigIntType })
  _id!: string
}


@Entity()
class Wallet {
  [PrimaryKeyType]: [string, string]

  @PrimaryKey()
  currencyRef!: string

  @ManyToOne({ primary: true, entity: () => User })
  owner!: User

  @Property({ type: String, nullable: false, default: "0" })
  mainBalance!: string
}


class AbstractDeposit {
  @Property({ type: String, nullable: false })
  amount!: string

  @Property({ type: String, nullable: false })
  gatewayKey!: string

  @Property()
  createdAt: Date = new Date()

  @Property({ onUpdate: () => new Date() })
  updatedAt: Date = new Date()
}

enum DepositStatus {
  UNPAID = "UNPAID",
  PENDING = "PENDING",
  COMPLETED = "COMPLETED",
  FAILED = "FAILED"
}


@Entity()
@Unique({ properties: ["gatewayKey", "txRef"] })
export class Deposit extends AbstractDeposit {
  [PrimaryKeyType]: [string, string, string]

  @PrimaryKey()
  txRef!: string

  @ManyToOne({ primary: true, entity: () => Wallet })
  wallet!: Wallet

  @Enum({
    nullable: false,
    items: () => DepositStatus
  })
  status: DepositStatus = DepositStatus.UNPAID
}

Versions

Dependency Version
node v14.15.0
typescript ^4.0.5
mikro-orm ^4.3.0
your-driver @mikro-orm/postgresql ^4.3.0
@dystopiandev dystopiandev changed the title Composite primary key consisting of composite primary keys Composite primary key consisting of composite primary key Nov 15, 2020
@B4nan B4nan added the bug Something isn't working label Nov 15, 2020
@B4nan
Copy link
Member

B4nan commented Nov 23, 2020

Nah this should be definitely fixable, just didn't have time to look into it yet (as I am moving to new flat these days :]). Will try to look into it during this week.

@dystopiandev
Copy link
Author

Nah this should be definitely fixable, just didn't have time to look into it yet (as I am moving to new flat these days :]). Will try to look into it during this week.

Sorry, deleted my older comment that asked if this was easily fixable by mistake. Thanks for the update.

@B4nan B4nan closed this as completed in b5f19f2 Dec 2, 2020
@B4nan
Copy link
Member

B4nan commented Dec 2, 2020

Fixed in 4.3.3-dev.14 (will be available in few minutes).

Note that you have wrong property name in your code:

      em.create(Deposit, {
        wallet,
        depositGatewayKey: "STRIPE", // the key is `gatewayKey` in your entity definition
        amount: "98765",
        txRef: UUIDv4()
      })

@dystopiandev
Copy link
Author

Oh, nevermind that typo. I refactored and got that mixed up while filing this issue.

Thanks a lot. Been waiting on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants