Skip to content

Loading…

Bug when selecting default value in CPgsqlSchema.php #2247

Open
hambai opened this Issue · 1 comment

2 participants

@hambai

In db/schema/pgsql/CPgsqlSchema.php there is a small bug. It uses the "pg_attrdef.adsrc" field but in PostgreSQL documentation ( http://www.postgresql.org/docs/8.3/static/catalog-pg-attrdef.html ) there is the following note:

The adsrc field is historical, and is best not used, because it does not track outside changes that might affect the representation of the default value. Reverse-compiling the adbin field (with pg_get_expr for example) is a better way to display the default value.

It looks like this problem occurs when you rename a schema or a table and the value of adsrc doesn't change. In my case the save method of the model raised exception of invalid schema name. The problem was that it was trying to retrieve the next id using the generator id from the old schema.

Since it is a one line fix, I'm pasting diff here but I'm ready to make a pull request if necessary.

--- db/schema/pgsql/CPgsqlSchema.php.orig   2013-03-22 15:48:49.001752205 +0200
+++ db/schema/pgsql/CPgsqlSchema.php    2013-03-22 15:49:12.533752265 +0200
@@ -163,7 +163,7 @@
    protected function findColumns($table)
    {
        $sql=<<<EOD
-SELECT a.attname, LOWER(format_type(a.atttypid, a.atttypmod)) AS type, d.adsrc, a.attnotnull, a.atthasdef
+SELECT a.attname, LOWER(format_type(a.atttypid, a.atttypmod)) AS type, pg_get_expr(d.adbin, a.attrelid) as adsrc, a.attnotnull, a.atthasdef
 FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
 WHERE a.attnum > 0 AND NOT a.attisdropped
    AND a.attrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname=:table
@resurtm

It looks like this problem occurs when you rename a schema or a table and the value of adsrc doesn't change. In my case the save method of the model raised exception of invalid schema name. The problem was that it was trying to retrieve the next id using the generator id from the old schema.

Would be great if you will try to provide unit tests which reproduces and checks this behavior.

Since it is a one line fix, I'm pasting diff here but I'm ready to make a pull request if necessary.

Feel free to make pull requests even for a tiny changes. It often happens that in addition to the main initial code enhancements and changes should be made after discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.