Code First

Define the schema in Kotlin

Code First means the KPojo class is the source of table metadata. Add table, column, primary key, index, and strategy annotations to the Kotlin model, then let table operations render DDL for the current database dialect.

User.kt

import com.kotlinorm.annotations.Column
import com.kotlinorm.annotations.ColumnType
import com.kotlinorm.annotations.NonNull
import com.kotlinorm.annotations.PrimaryKey
import com.kotlinorm.annotations.Table
import com.kotlinorm.enums.KColumnType
import com.kotlinorm.interfaces.KPojo

@Table("tb_user")
data class User(
    @PrimaryKey(identity = true)
    var id: Int? = null,
    @Column("user_name")
    @ColumnType(KColumnType.VARCHAR, 128)
    @NonNull
    var name: String? = null,
    var email: String? = null
) : KPojo

For focused mapping rules, see 📚KPojo, 📚Table and Column, 📚Primary Key, and 📚Indexes.

Create the table from KPojo metadata

Use wrapper.table.createTable(...) when the table does not exist yet. The active wrapper determines the database dialect.

create table

wrapper.table.createTable(User())

MySQL

CREATE TABLE `tb_user` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_name` VARCHAR(128) NOT NULL,
    `email` VARCHAR(255)
)

createTable(...) creates from the current KPojo shape. Use 📚Table Operations for drop, truncate, CTAS, and generated task details.

Synchronize an existing table

Use syncTable(...) when the table already exists and should be aligned with the KPojo definition.

sync table

val existed = wrapper.table.syncTable(User())

syncTable(...) returns false when it creates a missing table, and true when it compares an existing table and applies the required DDL.

For schema synchronization behavior and limits, see 📚Schema Sync.

Use Database First when the database owns the schema

Use Code First when Kotlin models are the source of truth. Use 📚Database First and 📚Codegen when existing database metadata should generate KPojo classes.