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

Better customisation of sequences #1280

Closed
rtc11 opened this issue Jun 22, 2021 · 2 comments · Fixed by #2197
Closed

Better customisation of sequences #1280

rtc11 opened this issue Jun 22, 2021 · 2 comments · Fixed by #2197

Comments

@rtc11
Copy link

rtc11 commented Jun 22, 2021

Today the sequence will start on 0:

val createSequence = autoIncColumn?.autoIncColumnType?.autoincSeq?.let { Sequence(it, startWith = 0, minValue = 0, maxValue = Long.MAX_VALUE).createStatement() }.orEmpty()

Would be nice to support customisation of the sequence:

    override val id = integer("id").autoIncrement(name = "awesome_seq", startWith = 1_000_000).entityId()

I have to start my sequences with a higher start value like 1_000_000.

I could use Flyway to solve this easily, but then the DDLs from exposed and flyway will differ. I really dont want to be this dependent on Flyway as I dont see how they will stay relevant for much longer (in the kotlin world)

@bog-walk
Copy link
Member

Hi @rtc11 , in the event that you are using a database that supports CREATE SEQUENCE operation (like PostgreSQL), you could set this up yourself if you're in a bind. Something like this for example:

object Tester : IdTable<Int>("tester") {
    override val id = integer("id").autoIncrement("large_starter_seq").entityId()
    override val primaryKey = PrimaryKey(id)
}

transaction {
    // ...
    val seq = Sequence("large_starter_seq", startWith = 1_000_000)
    SchemaUtils.createSequence(seq)
    SchemaUtils.create(Tester)

    repeat(5) {
        Tester.insert { }
        // INSERT INTO tester (id) VALUES (NEXTVAL('large_starter_seq'))
    }

    Tester.selectAll().map { it[tester.id] }
    // [1000000, 1000001, 1000002, 1000003, 1000004]

    SchemaUtils.drop(Tester)
}

I'm still going to tag this as a nice-to-have enhancement for all other db, so a delayed thank you for this suggestion!

@bog-walk
Copy link
Member

It will be possible to provide a custom sequence instance to autoIncrement() in the upcoming version 0.54.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants