Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks API #31

Open
p-gw opened this issue Dec 2, 2020 · 5 comments
Open

Callbacks API #31

p-gw opened this issue Dec 2, 2020 · 5 comments
Assignees

Comments

@p-gw
Copy link

p-gw commented Dec 2, 2020

Describe the bug
If callback fields are specified in the model, entries cannot be saved to the database. The save! function expects the callback fields of the struct as columns in the database table.

Error stacktrace

[ Info: 2020-12-01 19:17:36 Do something before saving
[ Info: 2020-12-01 19:17:36 INSERT INTO callbacks ( "title", "indicator", "created_at", "before_save", "after_save" ) VALUES ( E'testing', true, E'2020-12-01T19:17:36.376', E'#4', E'#5' ) RETURNING id
[error | LibPQ]: UndefinedColumn: ERROR:  column "before_save" of relation "callbacks" does not exist
LINE 1: ...O callbacks ( "title", "indicator", "created_at", "before_sa...
                                                             ^
ERROR: UndefinedColumn: ERROR:  column "before_save" of relation "callbacks" does not exist

To reproduce

  1. Create a model containing callbacks, e.g.
@kwdef mutable struct Callback <: AbstractModel
  id::DbId = DbId()
  title::String = ""
  indicator::Bool = true
  created_at::String = string(Dates.now())
  # callbacks
  before_save::Function = (m::Callback) -> begin
    @info "Do something before saving"
  end
  after_save::Function = (m::Callback) -> begin
    @info "Do something after saving"
  end
end
  1. Save entry to database
julia> Callback(title = "testing") |> save!

This will result in the above UndefinedColumn error.

Expected behavior
The expected behaviour is to ignore the callback fields (before_save and after_save) of the struct when creating entries in the database.

@FrankUrbach
Copy link
Contributor

FrankUrbach commented Dec 2, 2020

Hallo Phillip
Which adapter for storing your data you use? Some things are regulated within the adapter so it is important for a fast solution to know which one you use.
Edit: I see in the stacktrace you use the PostgeSQL-adapter.
Best Frank

@p-gw
Copy link
Author

p-gw commented Dec 2, 2020

I've tried it with both the PostgreSQL adapter and the SQLite adapter with the same result.

@FrankUrbach
Copy link
Contributor

It doesn't matter which one for solving this problem. Only when you have a problem with a task in one ore the other adapter for production purposes it should be the right adapter to begin with. :-)

@FrankUrbach
Copy link
Contributor

@adrian, @phillip
There are two different approaches for dealing with functions such as before_save, after_save and so on. First: The implementation like it is done for now in SearchLight by using a field in the model struct and dealing then with them. Second: We could implement this functions as a generic function, prepare fallback functions for this and let the user of SearchLIght choose whether he/she implement these functions for the type in the model.
For now I defined the fields which are reserved for this actions in an array and filter them on the right place. You will find the corresponding source file of src/SearchLight.jl attached to this comment.

Feel free to contact me any time if you have comments, wishes or hints.

SearchLight.jl.zip

Best Frank
P.S. I also removed an error from the save! chain.

FrankUrbach added a commit to FrankUrbach/SearchLight.jl that referenced this issue Dec 2, 2020
…'t be saved to database if callbacks are specified“
@essenciary essenciary changed the title Entries can't be saved to database if callbacks are specified Callbacks API Dec 31, 2020
@essenciary essenciary self-assigned this Dec 31, 2020
@essenciary
Copy link
Member

I think the callbacks API needs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants