Discard values ​​when inserting and updating data with a spot

I am using slick with play2. I have several fields in a database that are managed by a database. I do not want to create or update them, but I want to get them by reading the values.

For example, suppose that

case class MappedDummyTable(id: Int, .. 20 other fields, modified_time: Optional[Timestamp])

which displays the Dummy in the database. modified_time is database driven.

The problem is during insert or update , I create an instance of MappedDummyTable without the changed time attribute and pass it to slick to create / update, for example

TableQuery[MappedDummyTable].insert(instanceOfMappedDummyTable)

To do this, Slick creates a request as

 Insert INTO MappedDummyTable(id,....,modified_time) Values(1,....,null) 

and updates modified_time as NULL, which I don't want. I want Slick to ignore fields when updating and creating.

For the update I can do

TableQuery[MappedDummyTable].map(fieldsToBeUpdated).update(values)

but this leads to 20 odd fields in the map method, which looks ugly.

Is there a better way?

Update:

The best solution I found was to use multiple projections. I created one forecast to get the values, and the other to update and insert the data.

+5
source share
2 answers

Perhaps you need to write several triggers in the table if you do not want to write code like row => (row.id,...other 20 fields)

or try using None instead of null ?

0
source

I believe that a solution with displaying a custom field is the only way to do this using Slick. To make it less ugly, you can define an ignoreDefaults function on a MappedDummyTable that will only return the value and non-default function in a companion object to the case MappedDummyTable class, which returns the projection

 TableQuery[MappedDummyTable].map(MappedDummyTable.ignoreDefaults).insert(instanceOfMappedDummyTable.ignoreDefaults) 
0
source

Source: https://habr.com/ru/post/1206808/


All Articles