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

Dataclass Mapping changed in sqalchemy 2.0 #248

Open
PaleNeutron opened this issue Feb 2, 2023 · 8 comments
Open

Dataclass Mapping changed in sqalchemy 2.0 #248

PaleNeutron opened this issue Feb 2, 2023 · 8 comments

Comments

@PaleNeutron
Copy link

Refer: https://docs.sqlalchemy.org/en/20/orm/dataclasses.html#orm-declarative-native-dataclasses

Current documented dataclass style is:

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import MappedAsDataclass


class Base(MappedAsDataclass, DeclarativeBase):
    """subclasses will be converted to dataclasses"""


class User(Base):
    __tablename__ = "user_account"

    id: Mapped[int] = mapped_column(init=False, primary_key=True)
    name: Mapped[str]

or at least:

from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import registry


reg = registry()


@reg.mapped_as_dataclass
class User:
    __tablename__ = "user_account"

    id: Mapped[int] = mapped_column(init=False, primary_key=True)
    name: Mapped[str]

Columns type should be Mapped[int] not int which is different with current sqlacodegen and the new mapped_column is much more readable.

@agronholm
Copy link
Owner

Yeah, this needs updating. I'm not going to be working on this project for a little while (I have a queue of more urgent issues to address) so a PR would go a long way towards addressing this.

@amacfie
Copy link

amacfie commented Aug 3, 2023

Try latest master

@Franky1
Copy link

Franky1 commented Nov 13, 2023

Or try RC versions from pypi, for example: pip install "sqlacodegen==3.0.0rc3"
https://pypi.org/project/sqlacodegen/#history

@NixBiks
Copy link

NixBiks commented Nov 16, 2023

init=False is not being set in dataclasses generator for automatic id's e.g. (and relations). I think that's a mistake, no?

@agronholm
Copy link
Owner

How then would you explicitly set the primary key value on insert?

@NixBiks
Copy link

NixBiks commented Nov 16, 2023

True. But how would you let the server set it otherwise? You are not allowed to make an instance without the id

@agronholm
Copy link
Owner

This is a dilemma I haven't been able to figure out yet. As you probably know, SQLAlchemy really lets you do it either way, and it's just the annotations that are the issue here.

@NixBiks
Copy link

NixBiks commented Nov 16, 2023

Yeah I was just trying to figure out if it's possible. Doesn't seem like it. They don't really mention anything about it in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants