Using unique in a returning with sentinel looses the sentinel filter
This is likely an edge case, but it's still a bug
import sqlalchemy as sa
metadata = sa.MetaData()
t = sa.Table(
"t",
metadata,
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("data", sa.String(20)),
)
e = sa.create_engine(
"postgresql+psycopg2://scott:[email protected]:5432/test" # , echo=True
)
with e.connect() as c:
metadata.create_all(c)
r = c.execute(
t.insert().returning(t.c.data, sort_by_parameter_order=True),
[{"data": "a"}, {"data": "b"}],
)
print(r.all())
r = c.execute(
t.insert().returning(t.c.data, sort_by_parameter_order=True),
[{"data": "a"}, {"data": "b"}],
)
print(r.unique().all())
the above prints
[('a',), ('b',)]
[('a', 3), ('b', 4)]
while it should print [('a',), ('b',)] twice.
The issue is likely that CursorResult does not set sentinel_filter as the tuple filter of the metadata
This happens in any case where "generative" is used, so also if using yield_per(1) instead of unique()
seems simple to fix
nobody would notice this if it requires sort_by_parameter_order to happen because nobody is using that parameter directly
interestingly the processors already take into consideration the _num_sentinel_cols so if at least a column defines one the result is correct (since at the moment the length of the processors is used to define the length of the data)
Federico Caselli has proposed a fix for this issue in the main branch:
Improve handling of sentinel columns https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5039
Federico Caselli has proposed a fix for this issue in the rel_2_0 branch:
Improve handling of sentinel columns https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5040