schema-evolution: postgres introspection bug

11 views
Skip to first unread message

Matthew Flanagan

unread,
Oct 31, 2006, 11:37:20 PM10/31/06
to django-d...@googlegroups.com
Hi,

I've been testing out the schema-evolution branch again and hit the
same issue I was having before where it was trying to evolve a model
that hadn't changed. Here are my minimal test case models for this
issue:

from django.db import models

class FooType(models.Model):
name = models.CharField(maxlength=64, unique=True)

class Foo(models.Model):
name = models.CharField(maxlength=64)
foo_type = models.ForeignKey(FooType)

running './manage.py sqlevolve myapp' outputs:

BEGIN;
ALTER TABLE "myapp_foo" ADD COLUMN "name_tmp" varchar(64);
UPDATE "myapp_foo" SET "name_tmp" = "name";
ALTER TABLE "myapp_foo" DROP COLUMN "name";
ALTER TABLE "myapp_foo" RENAME COLUMN "name_tmp" TO "name";
ALTER TABLE "myapp_foo" ALTER COLUMN "name" SET NOT NULL;
COMMIT;

Note that if I rename the FooType model to BarType and do a fresh
install of my application again then run './manage.py sqlevolve myapp'
it outputs the expected:

BEGIN;
COMMIT;

Same goes if I rename the Foo.name field to something else.

This led me to think that some code somewhere is not matching on the
full table name. After a bit of digging I ended up at the
get_known_column_flags() function in
django/db/backends/postgresql/introspection.py on line 100 it reads:

cursor.execute("select pg_constraint.conname, pg_constraint.contype,
pg_attribute.attname from pg_constraint, pg_attribute where
pg_constraint.conrelid=pg_attribute.attrelid and
pg_attribute.attnum=any(pg_constraint.conkey) and
pg_constraint.conname ~ '^%s'" % table_name )

Changing '^%s' to '^%s_' fixes it but I'm not sure if this is the
right way to do it.

Is it?

regards

matthew

Matthew Flanagan

unread,
Nov 1, 2006, 10:18:36 PM11/1/06
to django-d...@googlegroups.com

Nope it isn't. If i rename the FooType model to Foo_Type I get the
same issue as before. ie. sqlevolve

Back to my original models, FooType and Foo. The output from the above
select statement is:

# select pg_constraint.conname, pg_constraint.contype,


pg_attribute.attname from pg_constraint, pg_attribute where
pg_constraint.conrelid=pg_attribute.attrelid and
pg_attribute.attnum=any(pg_constraint.conkey) and

pg_constraint.conname ~ '^myapp_foo';
conname | contype | attname
------------------------------------+------------+---------
myapp_foo_pkey | p | id
myapp_footype_pkey | p | id
myapp_footype_name_key | u | name
(3 rows)

This is where I reach the boundaries of my postgres knowledge.

Any help would be appreciated.

thanks

>
> regards
>
> matthew
>

mario__

unread,
Dec 8, 2006, 9:19:51 PM12/8/06
to Django developers

What do you want to do? I'm trying to see what you need and I'm not
quite shure this is the correct way to do that. When you want to search
data in the postgres catalog, there are some VIEWS you can use.

so, What do you need? Regards!

>

Reply all
Reply to author
Forward
0 new messages