Skip to content

Instantly share code, notes, and snippets.

@ezarowny
Last active April 12, 2023 14:42
Show Gist options
  • Save ezarowny/75c929f0dc705bd041c74920d084d200 to your computer and use it in GitHub Desktop.
Save ezarowny/75c929f0dc705bd041c74920d084d200 to your computer and use it in GitHub Desktop.
Traceback and sample code for pg_conn error
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/env/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "/env/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/core/management/commands/populate_search_vectors.py", line 48, in handle
Model.objects.build_search_vector_annotations().update(
File "/env/lib/python3.11/site-packages/django/db/models/query.py", line 1206, in update
rows = query.get_compiler(self.db).execute_sql(CURSOR)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1982, in execute_sql
cursor = super().execute_sql(result_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1547, in execute_sql
sql, params = self.as_sql()
^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1954, in as_sql
sql, params = self.compile(val)
^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 544, in compile
sql, params = node.as_sql(self, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/models/expressions.py", line 680, in as_sql
sql, params = compiler.compile(self.lhs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 544, in compile
sql, params = node.as_sql(self, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/contrib/postgres/search.py", line 149, in as_sql
sql = connection.ops.compose_sql(sql, config_params + params + extra_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/backends/postgresql/operations.py", line 205, in compose_sql
return mogrify(sql, params, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/django/db/backends/postgresql/psycopg_any.py", line 21, in mogrify
return ClientCursor(connection.connection).mogrify(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/psycopg/cursor.py", line 672, in __init__
super().__init__(connection)
File "/env/lib/python3.11/site-packages/psycopg/cursor.py", line 65, in __init__
self._pgconn = connection.pgconn
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'pgconn'
class State(models.Model):
"""A model representing a state."""
abbreviation = models.CharField(max_length=2)
name = models.CharField(max_length=254)
class City(models.Model):
"""
A model representing a city
"""
name = models.CharField(max_length=254)
search_vector = SearchVectorField(null=True)
state = models.ForeignKey("places.State", on_delete=models.PROTECT)
class Meta:
indexes = [GinIndex(fields=["search_vector"])]
@classmethod
def build_search_vector(cls):
return (
SearchVector("name", weight="B")
+ SearchVector("state_name", weight="A")
)
City.objects.annotate(
state_name=Subquery(
State.objects.filter(id=OuterRef("state_id")).values_list("name")[:1]
)
).update(
search_vector=Model.build_search_vector()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment