crecto icon indicating copy to clipboard operation
crecto copied to clipboard

Repo.insert stalls the process

Open zbaylin opened this issue 6 years ago • 10 comments

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

zbaylin avatar Jun 02 '18 16:06 zbaylin

This is a postgres database?

fridgerator avatar Jun 02 '18 18:06 fridgerator

Does the database log have any errors?

fridgerator avatar Jun 02 '18 19:06 fridgerator

This is a Postgres DB. The log has no errors.

zbaylin avatar Jun 02 '18 19:06 zbaylin

Are you using crecto master or 0.8.6 ?

fridgerator avatar Jun 02 '18 20:06 fridgerator

I assume master, because this is what I have in my shards.yaml:

dependencies:
  crecto:
    github: fridgerator/crecto

zbaylin avatar Jun 02 '18 20:06 zbaylin

I set the version to be 0.8.6 but the error still persists

zbaylin avatar Jun 02 '18 20:06 zbaylin

I can also confirm it has to do with the Array part of the insertion. If I remove that, the method completes normally.

zbaylin avatar Jun 03 '18 17:06 zbaylin

@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)

fridgerator avatar Jun 04 '18 01:06 fridgerator

My column data type is int[]

zbaylin avatar Jun 04 '18 15:06 zbaylin

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.

zbaylin avatar Jun 05 '18 20:06 zbaylin

I'm going to close this, but if anyone else experiences this issue feel free to re-open and we can explore it.

watzon avatar Dec 22 '23 18:12 watzon