Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new instance of the a entity without primary key?

Apologies is this is a dumb question. I'm trying to get familiar with Kotlin and came across an issue. I have a kotlin app where I store data using Room.

This is my entity class:

@Entity
data class Link(@PrimaryKey(autoGenerate = true) var _id: Int,
        @ColumnInfo(name = "link_url") var linkUrl: String?,
        @ColumnInfo(name = "timestamp") var timestamp: Long?)

How can I create a new instance of Link without specifying the _id?

ie

var link: Link = Link("url", 12334)

Thank you in advance!

like image 241
android enthusiast Avatar asked Jan 02 '23 13:01

android enthusiast


1 Answers

You can create another constructor with @Ignore annotation, so that it will be ignored by Room:

@Ignore
constructor(var linkUrl: String?, timestamp: Long?) : this (null, linkUrl, timestamp)

If you pass null for the autogenerated field, it will generate new value automatically.

like image 65
Angelina Avatar answered Jan 13 '23 15:01

Angelina