Assertion "schema is not None"
Hello:
I'm following the simple example from the project's front page:
https://pypi.org/project/Flask-SQLAlchemy/
but with a mariadb database.
```
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mariadb+pymysql://root:test_password_1234@localhost:3306"
class Base(DeclarativeBase):
pass
db = SQLAlchemy(app, model_class=Base)
class User(db.Model):
id: Mapped[int] = mapped_column(db.Integer, primary_key=True)
username: Mapped[str] = mapped_column(db.String, unique=True, nullable=False)
with app.app_context():
db.create_all()
db.session.add(User(username="example"))
db.session.commit()
users = db.session.execute(db.select(User)).scalars()
```
I get an AssertionError
```
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[2], line 10
8 username: Mapped[str] = mapped_column(db.String, unique=True, nullable=False)
9 with app.app_context():
---> 10 db.create_all()
12 db.session.add(User(username="example"))
13 db.session.commit()
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/flask_sqlalchemy/extension.py:900, in SQLAlchemy.create_all(self, bind_key)
883 def create_all(self, bind_key: str | None | list[str | None] = "__all__") -> None:
884 """Create tables that do not exist in the database by calling
885 ``metadata.create_all()`` for all or some bind keys. This does not
886 update existing tables, use a migration library for that.
(...) 898 Added the ``bind`` and ``app`` parameters.
899 """
--> 900 self._call_for_binds(bind_key, "create_all")
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/flask_sqlalchemy/extension.py:881, in SQLAlchemy._call_for_binds(self, bind_key, op_name)
878 raise sa_exc.UnboundExecutionError(message) from None
880 metadata = self.metadatas[key]
--> 881 getattr(metadata, op_name)(bind=engine)
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/sql/schema.py:5925, in MetaData.create_all(self, bind, tables, checkfirst)
5901 def create_all(
5902 self,
5903 bind: _CreateDropBind,
5904 tables: Optional[_typing_Sequence[Table]] = None,
5905 checkfirst: bool = True,
5906 ) -> None:
5907 """Create all tables stored in this metadata.
5908
5909 Conditional by default, will not attempt to recreate tables already
(...) 5923
5924 """
-> 5925 bind._run_ddl_visitor(
5926 ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
5927 )
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:3249, in Engine._run_ddl_visitor(self, visitorcallable, element, **kwargs)
3242 def _run_ddl_visitor(
3243 self,
3244 visitorcallable: Type[Union[SchemaGenerator, SchemaDropper]],
3245 element: SchemaItem,
3246 **kwargs: Any,
3247 ) -> None:
3248 with self.begin() as conn:
-> 3249 conn._run_ddl_visitor(visitorcallable, element, **kwargs)
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:2456, in Connection._run_ddl_visitor(self, visitorcallable, element, **kwargs)
2444 def _run_ddl_visitor(
2445 self,
2446 visitorcallable: Type[Union[SchemaGenerator, SchemaDropper]],
2447 element: SchemaItem,
2448 **kwargs: Any,
2449 ) -> None:
2450 """run a DDL visitor.
2451
2452 This method is only here so that the MockConnection can change the
2453 options given to the visitor so that "checkfirst" is skipped.
2454
2455 """
-> 2456 visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/sql/visitors.py:664, in ExternalTraversal.traverse_single(self, obj, **kw)
662 meth = getattr(v, "visit_%s" % obj.__visit_name__, None)
663 if meth:
--> 664 return meth(obj, **kw)
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/sql/ddl.py:960, in SchemaGenerator.visit_metadata(self, metadata)
956 else:
957 tables = list(metadata.tables.values())
959 collection = sort_tables_and_constraints(
--> 960 [t for t in tables if self._can_create_table(t)]
961 )
963 seq_coll = [
964 s
965 for s in metadata._sequences.values()
966 if s.column is None and self._can_create_sequence(s)
967 ]
969 event_collection = [t for (t, fks) in collection if t is not None]
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/sql/ddl.py:925, in SchemaGenerator._can_create_table(self, table)
923 if effective_schema:
924 self.dialect.validate_identifier(effective_schema)
--> 925 return not self.checkfirst or not self.dialect.has_table(
926 self.connection, table.name, schema=effective_schema
927 )
File <string>:2, in has_table(self, connection, table_name, schema, **kw)
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/engine/reflection.py:89, in cache(fn, self, con, *args, **kw)
87 info_cache = kw.get("info_cache", None)
88 if info_cache is None:
---> 89 return fn(self, con, *args, **kw)
90 exclude = {"info_cache", "unreflectable"}
91 key = (
92 fn.__name__,
93 tuple(
(...) 102 ),
103 )
File ~/p/Sketches/python/flask/venv/lib/python3.13/site-packages/sqlalchemy/dialects/mysql/base.py:2834, in MySQLDialect.has_table(self, connection, table_name, schema, **kw)
2831 if schema is None:
2832 schema = self.default_schema_name
-> 2834 assert schema is not None
2836 full_name = ".".join(
2837 self.identifier_preparer._quote_free_identifiers(
2838 schema, table_name
2839 )
2840 )
2842 # DESCRIBE *must* be used because there is no information schema
2843 # table that returns information on temp tables that is consistently
2844 # available on MariaDB / MySQL / engine-agnostic etc.
2845 # therefore we have no choice but to use DESCRIBE and an error catch
2846 # to detect "False". See issue #9058
AssertionError:
```
Environment:
- Python version: 3.13
- Flask-SQLAlchemy version:
- SQLAlchemy version:
```
> pip freeze
blinker==1.9.0
click==8.1.8
Flask==3.1.0
Flask-SQLAlchemy==3.1.1
greenlet==3.1.1
itsdangerous==2.2.0
Jinja2==3.1.6
MarkupSafe==3.0.2
PyMySQL==1.1.1
SQLAlchemy==2.0.40
typing_extensions==4.13.1
Werkzeug==3.1.3
```
关闭于 2025-04-08 1 条评论