question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Raise an exception if no SQLAlchemy session was set

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rbarroiscommented, Mar 12, 2016

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.

0reactions
jeffwidmancommented, Jan 7, 2017

Fixed by #339

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found