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.

DAO on compound/multiple primary key ?

See original GitHub issue

Hello,

PostgreSQL can use multiple fields as Primary Key (doc) but how to handle that with Exposed DAO ?

I tried to use the DAO with a table which is quite complexe. Because example is better than words. Here the SQL :

CREATE TABLE table__contacts_first
(
    contact_id    UUID         NOT NULL,
    app_id        VARCHAR(128) NOT NULL,

    --- other fields ...

    FOREIGN KEY (contact_id) REFERENCES table__contacts (contact_id),
    PRIMARY KEY (contact_id, app_id)
);

As you can see, the table__contacts_first doesn’t have any PK, but the pair contact_id + app_id acts as is. I have the following Exposed Table:

object ContactsFirstTable : Table(name = "table__contacts_first") {
    val contactId = reference("contact_id", ContactsTable).primaryKey()
    val appId = varchar("app_id", length = 128).primaryKey()
}

And now the Exposed DAO:

class ContactFirstDao(id: EntityID<UUID>) : UUIDEntity(id) {
    companion object : UUIDEntityClass<ContactFirstDao>(ContactsFirstTable)
}

Obviously, I got errors and in fact, I should construct my ContactFirstDao using contactId and appId.

Do you have any idea how I could achieve that ?

Thanks 👍

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:23
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
rodrigogscommented, Oct 13, 2021

So, no composite keys for Exposed?

I’m not calling it ORM anymore then. Composite keys are pretty basic!

5reactions
scorsicommented, Nov 26, 2020

An amaizing library: SQL ! I stopped using libraries, ORMs, … Just SQL is enough. But you can take a look at Ktorm (https://ktorm.liuwj.me/).

I left that issue open because it’s an importante missing feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Room @Relation with composite Primary Key - Stack Overflow
So we write a query in our Dao. @Query("select * from WorkOrder LEFT JOIN WorkOrderStaff ON woId = woIdStaff LEFT JOIN WorkOrderMachine ON...
Read more >
Spring Data JPA Composite Primary Key Mapping Example
A complete guide to map a composite primary key by using both @IdClass and @EmbeddedId annotations in Spring Data JPA.
Read more >
Can Multiple Primary Keys Exist on a Single Table? - Chartio
The short answer is no, a table is not allowed to contain multiple primary keys , as that goes against the fundamental principles...
Read more >
Configuring Entities - CommonsWare
Composite Primary Keys ... In some cases, you may have a composite primary key, made up of two or more columns in the...
Read more >
Create Composite Primary Key in VBA (DAO) - Tek-Tips
Here's where I'm stuck. I now need to set the fields CLASS / ITEM as a COMPOUND PRIMARY KEY. (I'm preparing, here, to...
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