ktorm icon indicating copy to clipboard operation
ktorm copied to clipboard

Adding empty entity does not insert into database

Open clydebarrow opened this issue 4 years ago • 3 comments

If the EntitySequence#add() function is called with an Entity that has no fields set, nothing is inserted into the database and the id of the entity is unchanged. In the example below the test fails at assertNotEquals(0, form.id) . If this assertion is removed, it then fails at form.flushChanges() with the error The entity is not associated with any database yet.

Tested on 3.2.0.

interface Form : Entity<Form> {
    companion object : Entity.Factory<Form>()

    var id: Int
    var name: String

}

open class Forms() : Table<Form>("forms") {

    companion object : Forms() {

        val id = int("id").primaryKey().bindTo { it.id }
        val name = varchar("name").bindTo { it.name }
    }

    fun create(): Form {
        val form = Form()
        database.sequenceOf(this).add(form)
        return form
    }
}
class FormsTest: BaseTest() {

    @Test
    fun formsTest() {
        val form = Forms.create()
        assertNotEquals(0, form.id)
        form.name = "name"
        form.flushChanges()
        assertEquals("name", form.name)
    }
}

clydebarrow avatar Oct 21 '20 20:10 clydebarrow

So what do you expect it should do? Insert an empty record to the database?

vincentlauvlwj avatar Oct 22 '20 16:10 vincentlauvlwj

I expect it to do exactly what the database would do for insert into tbl values(). E.g.

mysql> create table test (id int not null auto_increment primary key, name varchar(255) default 'dflt');

mysql> insert into test values();
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | dflt |
+----+------+

Silently doing nothing (especially when the documentation does not disclose this singular behaviour) is unexpected and IMHO unacceptable.

clydebarrow avatar Oct 22 '20 20:10 clydebarrow

I expect it to do exactly what the database would do for insert into tbl values().

Agree.

vincentlauvlwj avatar Oct 23 '20 02:10 vincentlauvlwj