Classical mapping, dataclasses with initialiser and filter clauses
See original GitHub issueHi,
I have come across some unexpected behaviour when using dataclasses mapped to SQLAlchemy using classical mapping.
Here’s a snippet with the essence of the issue:
from __future__ import annotations
from dataclasses import dataclass, field
from sqlalchemy import Column, Table, String, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import mapper, sessionmaker
Base = declarative_base()
metadata = Base.metadata
table = Table(
"table",
metadata,
#
Column("id", String, primary_key=True),
Column("active", Boolean),
)
@dataclass
class C:
id: str
active: bool = True
@dataclass
class C1:
id: str
active: bool
@dataclass
class C2:
id: str
active: bool = field(default_factory=lambda: True)
mapper(C, table)
mapper(C1, table)
mapper(C2, table)
Session = sessionmaker()
session = Session()
query = session.query(C)
print(query.filter(C.active == True))
print()
# Result (WRONG)
# SELECT "table".id AS table_id
# FROM "table"
# WHERE true
query = session.query(C1)
print(query.filter(C1.active == True))
print()
# Result: OK
# SELECT "table".id AS table_id, "table".active AS table_active
# FROM "table"
# WHERE "table".active = true
query = session.query(C2)
print(query.filter(C2.active == True))
print()
# Result: OK
# SELECT "table".id AS table_id, "table".active AS table_active
# FROM "table"
# WHERE "table".active = true
So, yes, I have found a workaround (using = field(default_factory=lambda: something)
as an initializer instead of just = something
), but it doesn’t look as good.
Is there some general advice regarding the interaction of dataclasses and SQLAlchemy ORM? (I have also tried with attrs, and got the same issue).
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:37 (28 by maintainers)
Top Results From Across the Web
How to structure dataclass with cattrs on sqlalchemy classical ...
So I did a little digging through the SQLAlchemy docs, and found & adapted an example that makes your code work: from dataclasses...
Read more >dataclasses — Data Classes — Python 3.11.1 documentation
This function is a decorator that is used to add generated special methods to classes, as described below. The dataclass() decorator examines the...
Read more >Axway Mapping Services DML User Guide
DML Mapping Project Information ... In turn, the possible data classes ... From the Business Document editor, click the Filter icon .
Read more >Essential Notation for Object-Relational Mapping
This thesis presents the Essential Notation for Object-Relational Mapping. (ENORM), a general purpose notation that represents structural concepts of Object ...
Read more >ORM Mapped Class Overview
The class to be mapped; The table, or other from clause object ... Changed in version 1.4: Declarative and classical mapping are now ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sure, I can try over the weekend. I want to look over the
dataclasses
documentation and implementation more closely first, to make sure I’m not missing something. But then I’ll try to come up with the tests.I didn’t like this idea at first but once I did it this might be a nicer way to go, it sure is tricky to set up these dataclasses though. See #5745 + the patch for complete POC it would be extremely helpful if you could test it out.