-
Not sure if this is a bug or I am doing something wrong, so wanted to ask first before opening an issue. I am trying to use Example: from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Integer, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
type_annotation_map = {str: String(36)}
pass
db = SQLAlchemy(model_class=Base)
migrate = Migrate(db=db)
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
db.init_app(app)
migrate.init_app(app)
class User(db.Model):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
username: Mapped[str] = mapped_column(unique=True)
email: Mapped[str | None] Error:
Am I doing something wrong? Is there a different way to do this? If I remove Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Does that example work in straight SQLAlchemy? |
Beta Was this translation helpful? Give feedback.
-
If someone faces the same error, from sqlalchemy.orm import DeclarativeBase, registry
class Base(DeclarativeBase):
registry = registry(type_annotation_map={str: String(36)}) Note that instead of having Since |
Beta Was this translation helpful? Give feedback.
If someone faces the same error,
flask-sqlalchemy
has some additional processing, probably for bind support. Not sure what is exact problem, but I saw some registry manipulation is being done and error message pointed to that, so I have solved the problem by doing the following:Note that instead of having
type_annotation_map
on class level, now registry is defined on class level withtype_annotation_map
parameter. This seems to work.Since
sqlalchemy
documentation mentions first approach andflask-sqlalchemy
documentation does not mention this (…