crecto
crecto copied to clipboard
Repo.insert stalls the process
I am running Crecto alongside Kemal and have the following route:
post "/dashboard/student/join_group" do |env|
begin
user = User.get_from_session(env)
group_query = Query
.where(group_code: env.params.body["group_code"].as(String))
.limit(1)
group = Repo.all(Group, group_query)[0]
membership = Membership.new
membership.user = user
membership.group = group
membership.periods = [] of Int64
periods = env.params.body["periods"].as(String)
.gsub(" ", "")
.split(",")
periods.each do |period|
membership.periods.as(Array).push(period.to_i64)
end
changeset = Repo.insert(membership)
if !changeset.valid?
raise "Invalid Data #{changeset.errors}"
end
env.flash["success"] = "You have joined group #{group.title}"
env.redirect "/dashboard/student"
rescue exception
p exception
env.flash["danger"] = "Unable to join group: #{exception}"
env.redirect "/dashboard/student"
end
end
The program completely freezes at the Repo.insert method, even though the record is inserted into the database.
Any help is appreciated.
EDIT:
Here is my membership model:
class Membership < Crecto::Model
set_created_at_field nil
set_updated_at_field nil
schema "user_groups", primary_key: false do
belongs_to :user, User
belongs_to :group, Group
field :periods, Array(Int64)
end
end
This is a postgres database?
Does the database log have any errors?
This is a Postgres DB. The log has no errors.
Are you using crecto master or 0.8.6 ?
I assume master, because this is what I have in my shards.yaml
:
dependencies:
crecto:
github: fridgerator/crecto
I set the version to be 0.8.6 but the error still persists
I can also confirm it has to do with the Array part of the insertion. If I remove that, the method completes normally.
@zbaylin what is your column type when you created the table?
Is it numeric[]
? If so, see here : https://github.com/will/crystal-pg/issues/105
If not: I've created a branch with this exact use case and a spec testing it.
The data class : https://github.com/Crecto/crecto/blob/insert_array_type/spec/spec_helper.cr#L60-L69
At this insert line, I get an exception Expected PQ::Frame::CommandComplete but got PQ::Frame::ReadyForQuery(@transaction_status=Idle)
My column data type is int[]
Ok I have fixed it (for now) by changing my datat type to bigint[]
. I will leave it up to @fridgerator if this issue should be closed or not because my fix only circumvents the problem.
I'm going to close this, but if anyone else experiences this issue feel free to re-open and we can explore it.