Ticket #7783: 7783-3.diff

File 7783-3.diff, 2.6 KB (added by Claude Paroz, 12 years ago)

Patch updated with test

  • django/db/backends/postgresql_psycopg2/introspection.py

    diff --git a/django/db/backends/postgresql_psycopg2/introspection.py b/django/db/backends/postgresql_psycopg2/introspection.py
    index 2ce579a..0027ec5 100644
    a b class DatabaseIntrospection(BaseDatabaseIntrospection):  
    3434
    3535    def get_table_description(self, cursor, table_name):
    3636        "Returns a description of the table, with the DB-API cursor.description interface."
     37        # As cursor.description does not return reliably the nullable property,
     38        # we have to query the information_schema (#7783)
     39        cursor.execute("""
     40            SELECT column_name, is_nullable
     41            FROM information_schema.columns
     42            WHERE table_name = %s""", [table_name])
     43        null_map = dict(cursor.fetchall())
    3744        cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
    38         return cursor.description
     45        return [tuple([item for item in line[:6]] + [null_map[line[0]]==u'YES'])
     46            for line in cursor.description]
    3947
    4048    def get_relations(self, cursor, table_name):
    4149        """
  • tests/regressiontests/introspection/models.py

    diff --git a/tests/regressiontests/introspection/models.py b/tests/regressiontests/introspection/models.py
    index da12f6e..3b8383e 100644
    a b class Reporter(models.Model):  
    55    first_name = models.CharField(max_length=30)
    66    last_name = models.CharField(max_length=30)
    77    email = models.EmailField()
    8     facebook_user_id = models.BigIntegerField()
     8    facebook_user_id = models.BigIntegerField(null=True)
    99
    1010    def __unicode__(self):
    1111        return u"%s %s" % (self.first_name, self.last_name)
  • tests/regressiontests/introspection/tests.py

    diff --git a/tests/regressiontests/introspection/tests.py b/tests/regressiontests/introspection/tests.py
    index 1835064..fa2b6c5 100644
    a b class IntrospectionTests(TestCase):  
    7878            ['IntegerField', 'CharField', 'CharField', 'CharField', 'BigIntegerField']
    7979        )
    8080
     81    def test_get_table_description_nullable(self):
     82        cursor = connection.cursor()
     83        desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
     84        self.assertEqual(
     85            [r[6] for r in desc],
     86            [False, False, False, False, True]
     87        )
     88
    8189    # Regression test for #9991 - 'real' types in postgres
    8290    @skipUnlessDBFeature('has_real_datatype')
    8391    def test_postgresql_real_type(self):
Back to Top