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.

Use value classes as ID

See original GitHub issue

Description

To prevent accidental using a wrong id from another entity, SQLDelight could generate an ID class for the primary key. For better performance, it should be a value class, if possible.

// before
val userID = 42
todos.find(id = userID) // compiles, but wrong ID

// after
val userID = User.ID(42)
todos.find(id = userID) // error: expected Todo.ID

With this definition, SQLDelight would generate this classes:

CREATE TABLE user (
id INT NOT NULL PRIMARY KEY
);
CREATE TABLE todo (
id INT NOT NULL PRIMARY KEY,
userID INT NOT NULL,

FOREIGN KEY (userID) REFERENCES user(id)
);
data class User(val id: User.ID) {
    value class ID(val value: Int)
}
data class Todo(val id: Todo.ID, val userID: User.ID) {
    value class ID(val value: Int)
}

BTW fluent-kit, an ORM framework written in Swift, provides this feature: http://github.com/vapor/fluent-kit/ Model file: https://github.com/vapor/fluent-kit/blob/main/Sources/FluentKit/Model/Model.swift

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AlecStrongcommented, Dec 8, 2021

you can do composite primary keys, we have some in cash

1reaction
eygrabercommented, Dec 2, 2021

You should be able to do this with custom column types. @JvmInline value class works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Effective Kotlin Item 49: Consider using inline value classes
This way, inline value classes allow us to introduce types where they were not allowed before; thanks to this, we have safer code...
Read more >
How I use the new Inline Value Classes in Kotlin - Manu Sobles
Learn to apply the concept of Value Objects from Domain-Driven Design in Kotlin by using value classes.
Read more >
Value Classes and Universal Traits - Scala Documentation
One use case for value classes is to combine them with implicit classes (SIP-13) ... the actual Meter instance must be created for...
Read more >
Value-based Classes
Value -based Classes. Some classes, such as java.util.Optional and java.time.LocalDateTime , are value-based. Instances of a value-based class:.
Read more >
Is there a basic id / value object in Java? - Stack Overflow
You want a class that constitutes a mutable int/String pair, and so your code is OK. but that's something for other languages.
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