You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using erd-editor as the design-first step in a FastAPI + PostgreSQL workflow. The code generator already covers DDL for the major SQL dialects which is great, but my next step after designing the schema is writing SQLAlchemy models. Currently I have to either write them by hand or run sqlacodegen against a live database.
A SQLAlchemy generator would close this loop entirely:
Looking at the existing code generator structure, it seems like this would follow the same pattern as the existing dialect .ts files, just outputting Python class definitions instead of SQL. For example, given a users table it would output something like:
Example
CREATETABLEusers
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
google_sub textNOT NULL UNIQUE,
email textNOT NULL UNIQUE,
email_verified booleanNOT NULL DEFAULT false,
created_at timestamptzNOT NULL DEFAULT now(),
last_login_at timestamptzNOT NULL DEFAULT now(),
PRIMARY KEY (id)
);
COMMENT ON TABLE users IS 'Stores authenticated users. Keyed on Google's sub claim.';COMMENT ON COLUMN users.id IS 'internal private key';COMMENT ON COLUMN users.google_sub IS 'Google's immutable user identifier from the `sub` claim';
COMMENT ON COLUMN users.email IS 'updated on every login in case user changes it';
COMMENT ON COLUMN users.email_verified IS 'from Google token, always true for Google auth';
COMMENT ON COLUMN users.created_at IS 'set once on provisioning, never updated';
COMMENT ON COLUMN users.last_login_at IS 'updated on every login';
"""ORM model for the users table."""importuuidfromdatetimeimportdatetimefromsqlalchemyimportBoolean, DateTime, Text, textfromsqlalchemy.dialects.postgresqlimportUUIDfromsqlalchemy.ormimportMapped, mapped_columnfromsqlalchemy.sqlimportfuncfromapp.db.baseimportBaseclassUser(Base):
"""Represents a user who has authenticated via Google OAuth2."""__tablename__="users"id: Mapped[uuid.UUID] =mapped_column(
UUID(as_uuid=True),
primary_key=True,
server_default=text("gen_random_uuid()"),
)
google_sub: Mapped[str] =mapped_column(Text, nullable=False, unique=True)
email: Mapped[str] =mapped_column(Text, nullable=False, unique=True)
email_verified: Mapped[bool] =mapped_column(
Boolean, nullable=False, server_default=text("false")
)
created_at: Mapped[datetime] =mapped_column(
DateTime(timezone=True), nullable=False, server_default=func.now()
)
last_login_at: Mapped[datetime] =mapped_column(
DateTime(timezone=True), nullable=False, server_default=func.now()
)
Happy to contribute this if pointed to the right place in the codebase.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
I'm using erd-editor as the design-first step in a FastAPI + PostgreSQL workflow. The code generator already covers DDL for the major SQL dialects which is great, but my next step after designing the schema is writing SQLAlchemy models. Currently I have to either write them by hand or run sqlacodegen against a live database.
A SQLAlchemy generator would close this loop entirely:
Looking at the existing code generator structure, it seems like this would follow the same pattern as the existing dialect
.tsfiles, just outputting Python class definitions instead of SQL. For example, given auserstable it would output something like:Example
Happy to contribute this if pointed to the right place in the codebase.
All reactions