Comments (16)
hi -
this is a very clear, straightforward bug report with a very clear assertion which then...does not reproduce for me at all. the query at the end has the extra criteria and we have a lot of tests for this:
SELECT address.id
FROM user_account JOIN address ON address.id = user_account.address_id
WHERE user_account.model_type IN (__[POSTCOMPILE_model_type_1])
full test
from sqlalchemy import select
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "user_account"
id: Mapped[int] = mapped_column(primary_key=True)
address_id: Mapped[int] = mapped_column(ForeignKey("address.id"))
model_type: Mapped[str] = mapped_column(String(50))
__mapper_args__ = {"polymorphic_identity": "base", "polymorphic_on": "model_type"}
class Admin(User):
__mapper_args__ = {"polymorphic_identity": "admin"}
class Address(Base):
__tablename__ = "address"
id: Mapped[int] = mapped_column(primary_key=True)
print(select(Address.id).select_from(Admin).join(Address))
from sqlalchemy.
here's sqlite
SELECT address.id
FROM user_account JOIN address ON address.id = user_account.address_id
WHERE user_account.model_type IN (?)
2024-05-24 10:39:33,361 INFO sqlalchemy.engine.Engine [generated in 0.00067s] ('admin',)
postgresql
SELECT address.id
FROM user_account JOIN address ON address.id = user_account.address_id
WHERE user_account.model_type IN (%(model_type_1_1)s)
2024-05-24 10:40:07,564 INFO sqlalchemy.engine.Engine [generated in 0.00009s] {'model_type_1_1': 'admin'}
do you have any event handlers modifying queries ? or is that not an accurate version of your real world test?
from sqlalchemy.
Odd! I reproduced locally and then again in a python fiddle with my minimalist example: https://python-fiddle.com/saved/Mf8Y0oUm37DfVyOINqjQ
I'm still able to reproduce with your example
from sqlalchemy.
has your SQLAlchemy installation been modified? can you verify you're on 2.0.30? it looks like older 2.0.x versions do have this bug, so likely an upgrade needed
from sqlalchemy.
Confirming my local version now. Indeed the python fiddle is using 2.0.20
from sqlalchemy.
it looks like this behavior changed in 2.0.22 with #10365 . so there's something concerning which is that that issue wasn't targeting this behavior so we may need to add a more direct test
from sqlalchemy.
I'm still seeing the issue locally on 2.0.30 but my local configuration is much more complex, which may or may not be a contributing factor. I'll try and match your reproduction and go from there. Thanks for the quick feedback.
from sqlalchemy.
Mike Bayer has proposed a fix for this issue in the main branch:
Add test for issue 11412 https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5305
from sqlalchemy.
Mike Bayer has proposed a fix for this issue in the rel_2_0 branch:
Add test for issue 11412 https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5306
from sqlalchemy.
Mike Bayer has proposed a fix for this issue in the rel_1_4 branch:
Add test for issue 11412 https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5307
from sqlalchemy.
Ah so I can still replicate the issue with the legacy ORM syntax, but agreed the query syntax fix is good on 2.0.30.
Issue exists in:
print(session.query(Admin).join(Address).with_entities(Address.id))
from sqlalchemy.
Full reproduction on 2.0.30
from sqlalchemy import select
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Query
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "user_account"
id: Mapped[int] = mapped_column(primary_key=True)
address_id: Mapped[int] = mapped_column(ForeignKey("address.id"))
model_type: Mapped[str] = mapped_column(String(50))
__mapper_args__ = {"polymorphic_identity": "base", "polymorphic_on": "model_type"}
class Admin(User):
__mapper_args__ = {"polymorphic_identity": "admin"}
class Address(Base):
__tablename__ = "address"
id: Mapped[int] = mapped_column(primary_key=True)
# this includes the WHERE clause (good, not a bug)
print(select(Address.id).select_from(Admin).join(Address))
# this is missing the WHERE clause (incorrect, this is a bug)
print(Query(Admin).join(Address).with_entities(Address.id))
from sqlalchemy.
that's expected, with_entities()
replaces what you passed to Query()
as though it never happened.
use select_from() here as well
print(Query(Admin).select_from(Admin).join(Address).with_entities(Address.id))
from sqlalchemy.
OK I see. I find it odd that it still retains the reference to the underlying table user_account
but in either case, easy for us to be aware of (and we should migrate to the newer syntax anyways).
Thanks for the time/help!
from sqlalchemy.
Query converts to a select() internally but only at the end. it's all legacy stuff that we dont really want to mess with if we dont have to.
from sqlalchemy.
Understood!
from sqlalchemy.
Related Issues (20)
- The get_multi_indexes method in PGDialect does not work correctly with pgvector-rs HOT 1
- Recurring critical SQLAlchemy errors are causing DB connection failure HOT 1
- Permanently corrupted hybrid_property column labeling as a result of selecting column returned by hybrid_property.expression HOT 8
- Handle ORM compatible querying for non-session connections HOT 1
- dupe key logic for mysql 8 is causing failures since "AS new" should not render withotu a VALUES clause HOT 2
- Adding ADBC Driver as engine option HOT 1
- SQLite reflection misses UNIQUE constraint when there are tabs in the table DDL HOT 2
- Error reflecting postgresql identity columns
- Import is very slow. Takes about 0.35 seconds which is too much. HOT 1
- mypy errors when using hybrid_property
- Support empty connection string for postgresql+psycopg HOT 6
- asyncio: cancelling a task that performs insert operations in parallel can raise unexpected errors HOT 3
- Support limit using with_dialect_options in delete in Mysql HOT 2
- and_ only acceptiong Literal[True] instead of bool HOT 1
- can not sort result when filter has boolean condition HOT 3
- Add sqlalchemy.orm.Session.merge_all / delete_all HOT 8
- Typing lost when using selectable.with_only_columns(x, maintain_column_froms=...) HOT 5
- Memory leak? orm.clsregistry._ModuleMarker._remove_item() HOT 2
- schematype.metadata is not re-assigned on tometadata HOT 2
- I am getting File "src/pymssql/_pymssql.pyx", line 479, in pymssql._pymssql.Cursor.execute pymssql._pymssql.OperationalError: (3971, b'The server failed to resume the transaction. Desc:4900000003.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n') HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlalchemy.