Raise an exception if no SQLAlchemy session was set
See original GitHub issueI just inherited from the wrong class, so no session
attribute was set. Instead of a clear excetption, the code failed implicitly when the object was tried to be added to the session.
It would be a nice convenience feature to raise a ValueError or similiar.
Proof of concept:
from factory.alchemy import SQLAlchemyModelFactory as Factory
from factory.fuzzy import FuzzyText
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import (Column, String)
db = SQLAlchemy()
class Traffic(db.Model):
traffic_id = Column(String(15), nullable=False, index=True, primary_key=True)
class TrafficFactory(Factory):
class Meta:
model = Traffic
# this was missing:
# session = db.session
traffic_id = FuzzyText()
if __name__ == '__main__':
assert TrafficFactory()
Returns:
AttributeError: 'NoneType' object has no attribute 'add'
The place where it fails is alchemy.py:46.
Adding if not session: raise ValueError("No session provided")
before that line should be sufficient.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
SQLAlchemy session after exception - python - Stack Overflow
In SQLAlchemy, after an exception, the session needs to be set again because of the rollback. Should I therefor always set the session...
Read more >Error Messages - SQLAlchemy 1.4 Documentation
Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc....
Read more >Core Exceptions - SQLAlchemy 1.4 Documentation
Raised when the execution of a database operation fails. Wraps exceptions raised by the DB-API underlying the database operation. Driver-specific ...
Read more >Session Basics — SQLAlchemy 2.0 Documentation
By “framing” we mean that if all operations succeed, the Session.commit() method will be called, but if any exceptions are raised, ...
Read more >ORM Exceptions — SQLAlchemy 1.4 Documentation
Exception types that may be raised by instrumentation implementations. ... target row based on primary key; if no row is returned, this exception...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just a note here: the session inheritance is handled in
factory.base.FactoryOptions
.For this case, it might be simpler to put the check with the
SQLAlchemyModelFactory._create
code, just before accessing the session.This ways, you don’t have to worry about the way sessions are passed across the stack, nor handling the
abstract=True
factories in that chain.Fixed by #339