46 ms·
The short log is missing one of the most important and awesome new feature to application writers: a bunch of new PQresultErrorFields[0] on constraint failure,
by masklinn 14y ago
The short log is missing one of the most important and awesome new feature to application writers: a bunch of new PQresultErrorFields[0] on constraint failure, providing access to (context-dependent) the raw schema name, table name, column name, constraint name and datatype involved.
Previously these remained locked into the database and to get programmatically-useable info (for logging, better error messages or to translate in terms of e.g. your ORM) you had to parse error messages which were potentially localized and usually lacked half the necessary information.
And even better, for python developers, psycopg2 already supports these fields[1][2]
[0] http://www.postgresql.org/docs/devel/static/libpq-exec.html#LIBPQ-PQRESULTERRORFIELD http://www.postgresql.org/docs/devel/static/libpq-exec.html#...
[1] http://psycopg.lighthouseapp.com/projects/62710/tickets/149 http://psycopg.lighthouseapp.com/projects/62710/tickets/149
[2] http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.Diagnostics http://initd.org/psycopg/docs/extensions.html#psycopg2.exten...
- btown 14y agoWhere would one find out if anyone is working on a patch for ActiveRecord?
- masklinn 14y agoThe Rails/ActiveRecord bug tracker? edit: the underlying connector might be a better idea to start with, as it'll need to expose the fields somehow before an ORM can take advantage of them.
- judofyr 14y agohttps://bitbucket.org/ged/ruby-pg https://bitbucket.org/ged/ruby-pg
- jeltz 14y agoI think it can use either pg (wrapper around libpq) or postgres-pr (pure ruby), though I always use pg. EDIT: Seems like they have dropped support for postgres. EDIT2: Yes, I did mean postgres-pr.
- masklinn 14y agoDo you mean postgres-pr? (postgres is marked as a deprecated gem to replace with pg)
- netghost 14y agoThis is where you'd want to be looking in the source: https://github.com/rails/rails/tree/master/activerecord/lib/active_record/connection_adapters/postgresql https://github.com/rails/rails/tree/master/activerecord/lib/... Specifically at any error handling, which I think would likely be here: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb https://github.com/rails/rails/blob/master/activerecord/lib/...
- joevandyk 14y agoLooks like work has started on the underlying connector: https://bitbucket.org/ged/ruby-pg/issue/161/add-support-for-pg_diag_-_name-error https://bitbucket.org/ged/ruby-pg/issue/161/add-support-for-...
- masklinn 14y agoI wouldn't say "work has started" when the bug has just been reported.
- effn 14y agoActiveRecord doesn't even support constraints in the first place.
- joevandyk 14y agoWhat do you mean by "support constraints"? If I, for example, violate a unique index, a ActiveRecord::RecordNotUnique exception is raised.
- mjhoy 14y agoIs that true? I might be wrong about this, but I think ActiveRecord checks for uniqueness (and other constraints) in application code, generating an extra database query if needed. If you _actually_ violate the unique index, a PGError is raised (if using postgres).
- FooBarWidget 14y agoNo. ActiveRecord checks for constraints on top of what the database offers. You are free to specify database constraints if you like. In older versions of ActiveRecord, uniqueness errors generate a generic database exception. In recent versions, uniqueness errors generate the specific exception RecordNotUnique.
- pjungwir 14y agoShameless plug: here is a gem I wrote to manage foreign keys and CHECK constraints (and also indexes) in a Rails project: https://github.com/pjungwir/db_leftovers https://github.com/pjungwir/db_leftovers
- moonlight80 14y agoI think ActiveRecord pattern is more limiting you than helpful. I found the following post quite interesting: http://mindref.blogspot.com/2013/02/sql-vs-orm.html http://mindref.blogspot.com/2013/02/sql-vs-orm.html
- papsosouid 14y agoWow, that is awesome and is something I've been worrying about recently. However, there is a second part that is still missing that would allow for actual postgresql application development. There needs to be a way to check all constraints and report all failures, rather than just stopping at the first error. Without that, users submit data, get an error for one field, fix it and resubmit, get an error for the next field, etc, etc. Reporting all the errors the first time is really important for usability. So right now you have to duplicate all the database constraints in your app, which is a huge source of code duplication (and thus bugs).
- jeffdavis 14y ago"check all constraints and report all failures" Interesting point. Foreign key failures are usually the result of a program bug. Unique and exclusion[1] constraints depend on concurrent actions, and can't be caught ahead of time as easily. So I assume you are mostly talking about CHECK constraints. Also, we're talking about a single statement, which probably means a single table. It does sound like a good idea to validate all of the check constraints on a table at once, and report all of the failures. [1] http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION http://www.postgresql.org/docs/current/static/ddl-constraint...
- papsosouid 14y agoYes and no. Yes in that I am talking specifically about check constraints when I say that we have to duplicate them all in our apps. You can't really duplicate the others. But no in that I don't mean to validate only check constraints and report all failures, ideally it would do unique and FK too. If there's an underlying reason that isn't feasible, then doing all the check constraints at once would certainly be a big improvement though. We're already in the position of having data validation type checks done separately from uniques now, so that would get users of our apps the current behaviour while letting us delete a bunch of code.
- plq 14y agoFWIW, we try very hard to not write any check constraints and do any such validation at the protocol level, before the request even hits the authn/authz layer. (Spyne makes it easy ! http://spyne.io http://spyne.io)