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

Support default expression value of another field in a table #1618

Closed
Nek-12 opened this issue Oct 29, 2022 · 6 comments
Closed

Support default expression value of another field in a table #1618

Nek-12 opened this issue Oct 29, 2022 · 6 comments

Comments

@Nek-12
Copy link

Nek-12 commented Oct 29, 2022

I want to match exposed schema declaration to the following sql:

alter table entry
add column entry_id uuid not null default (id); 
-- entry has a field named id

(this won't work with raw sql but is this possible with exposed?)

But how can I do this using exposed syntax, except for defining my own custom expression?
I couldn't find ways to convert column to Expression, couldn't do it using CustomFunction, haven't found any other ways

@AlexeySoshin
Copy link
Contributor

AlexeySoshin commented Oct 29, 2022

You should be able to do that using defaultExpression() syntax.

You didn't post your table definition, but I would expect that something like

object EntryTable : UUIDTable() {
    val entryId = uuid("entry_id").defaultExpression(EntryTable.id)
}

would work.

@Nek-12
Copy link
Author

Nek-12 commented Oct 29, 2022

@AlexeySoshin

I'm using an UUIDTable:

Type mismatch.
Required:
UUID
Found:
EntityID<UUID>

Type mismatch.
Required:
EntityID<UUID>
Found:
UUID

Both of these errors are displayed at the same time in the IDE

@AlexeySoshin
Copy link
Contributor

You're right, it won't work for PKs, since EntityID<UUID> != UUID

Probably having a custom expression is the way to go:

object EntryTable : UUIDTable() {
    val entryId = uuid("entry_id").defaultExpression(object : Expression<UUID>() {
        override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
            append("ID")
        }
    })
}

This generates the following SQL:

CREATE TABLE IF NOT EXISTS entry (id uuid PRIMARY KEY, entry_id uuid DEFAULT (ID) NOT NULL)

By the way, I'm curious, which database are you using?
At least PostgreSQL doesn't support this syntax:
cannot use column reference in DEFAULT expression

@Nek-12
Copy link
Author

Nek-12 commented Oct 30, 2022

Yeah I said that this won't work with sql, but I wanted to do this in runtime then. Wouldn't it be possible?

@AlexeySoshin
Copy link
Contributor

There's clientDefault in Exposed that could work theoretically, but I don't think would work practically.

To reference ID of a newly created row, you need to somehow obtain it first. Currently the default values are set independently from one another:

And I don't see any guarantees that your EntityID default value will be initialized first.

@Nek-12
Copy link
Author

Nek-12 commented Dec 3, 2022

Okay, I just settled on treating this functionality as impossible to implement using neither sql or orms of any kind. We can close this if we mention that this is not supported somewhere in the docs

@joc-a joc-a closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants