본문 바로가기

SQLAlchemy3

SpringDataJPA 일부 구현해보기 fetchJoin을 통해 eager loading을 사용하기 보다는 select 문에 객체들을 선언하고 명시적으로 조인을 사용하는 방식을 더 선호해서 사용하지 않지만, 깃허브에서 좋은 내용을 봐서 작성해봅니다.from typing import Generic, TypeVar, Any, Sequencefrom sqlalchemy.ext.asyncio import AsyncSessionfrom sqlalchemy import select, insertfrom .base import BaseEntityEntityType = TypeVar("EntityType", bound=BaseEntity)class BaseRepository(Generic[EntityType]): def __init__( .. 2024. 5. 5.
DTO, 객체 인스턴스 생성 파이썬 dict 자료 구조를 통해 객체 인스턴스를 생성할 때, 아래의 코드를 작성할 수 있다.args = { "a": "apple", "b": "banana",}dto = DTO(**args) SQLAlchemy에서도 위와 비슷한 코드 작성이 가능하다.# row는 SQLAlchemy의 ROW 객체dto = DTO(**row._mapping) 반대로 Row to dictdict(row._mapping) 2024. 3. 26.
2.0 요약 정리 session.query() 방식에서 session.execute() execute()의 리턴 타입은 Result object. Result object Named tuple과 유사한 인터페이스를 가진 Row object를 반환하는 Iterable function. Result object에 대해서 Iterating 없이 결과를 얻을 수 있는 방법 all() to return a list with a row object for each result row. 예) [row(값1, 값2, ...), row(값1, 값2, ...) , ...] SQL 또는 ORM에서 프로젝션을 하는 경우, 용이하다. for loop를 돌면서 select하는 컬럼의 별칭이나, 객체의 속성을 매핑하여 값을 확인할 수 있다. fir.. 2024. 2. 19.