ktorm
ktorm copied to clipboard
Adding empty entity does not insert into database
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)
}
}
So what do you expect it should do? Insert an empty record to the database?
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.
I expect it to do exactly what the database would do for
insert into tbl values()
.
Agree.