Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for nested composite types and allow schema specification #566

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions sqlalchemy_utils/types/pg_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ def __getattr__(self, key):

return CompositeElement(self.expr, key, type_)

def __init__(self, name, columns):
def __init__(self, name, columns, schema=None):
if psycopg2 is None:
raise ImproperlyConfigured(
"'psycopg2' package is required in order to use CompositeType."
)
SchemaType.__init__(self)
SchemaType.__init__(self, schema=schema)
self.name = name
self.columns = columns
if name in registered_composites:
Expand All @@ -217,7 +217,7 @@ def make(obj, values):
attach_composite_listeners()

def get_col_spec(self):
return self.name
return f'{self.schema}.{self.name}' if self.schema else self.name

def bind_processor(self, dialect):
def process(value):
Expand All @@ -238,6 +238,11 @@ def process(value):
current_value, dialect
)
)
elif isinstance(column.type, CompositeType) or isinstance(column.type, CompositeArray):
type_proc = column.type.dialect_impl(dialect).bind_processor(
dialect
)
processed_value.append(type_proc(current_value))
else:
processed_value.append(current_value)
return self.type_cls(*processed_value)
Expand Down Expand Up @@ -276,7 +281,7 @@ def drop(self, bind=None, checkfirst=True):

def register_psycopg2_composite(dbapi_connection, composite):
psycopg2.extras.register_composite(
composite.name,
composite.get_col_spec(),
dbapi_connection,
globally=True,
factory=composite.caster
Expand Down Expand Up @@ -304,7 +309,7 @@ def adapt_composite(value):
else value.getquoted()
for value in adapted
]
return AsIs("(%s)::%s" % (', '.join(values), composite.name))
return AsIs("(%s)::%s" % (', '.join(values), composite.get_col_spec()))

register_adapter(composite.type_cls, adapt_composite)

Expand Down