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

UNIQUE constraint causes from_rs to hang #46

Open
fridgerator opened this issue Mar 28, 2017 · 7 comments
Open

UNIQUE constraint causes from_rs to hang #46

fridgerator opened this issue Mar 28, 2017 · 7 comments

Comments

@fridgerator
Copy link
Contributor

fridgerator commented Mar 28, 2017

I wasn't sure whether to make this issue here, or in crystal-pg

Crystal: 0.21.1
Postgres: 9.6.2 (also replicated on 9.4)
crystal-db: 0.4.0
crystal-pg: 0.13.3

If there is a UNIQUE constraint on a database field, Model.from_rs(rs) hangs without returning.

require "pg"

class User
  DB.mapping({
    name: String?
  }, false)
end

db = DB.open("postgres://localhost:5432/crecto")

db.query "INSERT INTO users (name) VALUES ($1) RETURNING *", "test" do |rs|
	puts User.from_rs(rs)
end

db.query "INSERT INTO users (name) VALUES ($1) RETURNING *", "test" do |rs|
	puts User.from_rs(rs)
end

db.close

If I remove the UNIQUE constraint when I create the table, the above code works just fine.

@fridgerator
Copy link
Contributor Author

fridgerator commented Mar 28, 2017

If I switch back to: crystal-db: 0.3.3 and crystal-pg: 0.13.2

it correctly throws the duplicate key value violates unique constraint "users_name_key" (PQ::PQError) exception

@bcardiff
Copy link
Member

It's probably that the type of the exception is messing forcing the retries. IO::Error's are wrapped as DB::ConnectionLost to trigger a retry in some places like here. But PQ::PQError shouldn't be wrapped.

@fridgerator
Copy link
Contributor Author

I see

Should I open the issue on crystal-pg instead?

@bcardiff
Copy link
Member

I try to narrow it down. It seems an issue in crystal-pg.

I reduce a full script that repro the issue.
From a nasty puts debugging the call to PQ::Connection.read_next_row_start assigns 'E' instead of 'D' probably (?) in type = soc.read_char

I haven't go any further than this.

require "pg"

`psql -c "DROP DATABASE IF EXISTS issue_db_46"`
`createdb issue_db_46`
`psql -d issue_db_46 -c "CREATE TABLE users (name varchar(50), CONSTRAINT key_name UNIQUE(name));"`
`psql -d issue_db_46 -c "INSERT INTO users (name) VALUES ('test');"`

db = DB.open("postgres://localhost/issue-db-46")

puts "A"

db.query "INSERT INTO users (name) VALUES ($1) RETURNING *", "test" do |rs|
  rs.move_next
end

puts "Z"

db.close

@rdp
Copy link

rdp commented Apr 5, 2017

Perhaps you could find the offending commit in crystal-pg?

@fridgerator
Copy link
Contributor Author

Also hangs if autoincrement is left off of the primary id field and the user tries to insert a record. see crecto issue. It seems an exception should also be raised in this case, and seems this may be related to this issue.

@greenbigfrog
Copy link
Contributor

I think this has been fixed

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

No branches or pull requests

4 participants