Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlalchemy.orm.exc.DetachedInstanceError in unit test with socketio.test_client #1669

Closed
miguelgrinberg opened this issue Aug 24, 2021 Discussed in #1616 · 3 comments
Closed
Assignees
Labels

Comments

@miguelgrinberg
Copy link
Owner

Discussed in #1616

Originally posted by LostInDarkMath June 27, 2021
Describe the bug
I just included Flask-SocketIO in my project which is based on Flask-SQLAlchemy and wrote some tests. But I in a simple test I get the following error:

sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x2812f970> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/14/bhk3)

The error causing test can be reduced to the following:

def test_receive_notifications_on_new_message_via_websockets(self) -> None:
    socketio_client = self.socketio.test_client(app=self.app, flask_test_client=self.client, headers=self.header())
    self._send_message(sender=self.dummy2, receiver=self.dummy)  # it fails here

If I comment out the the first line that creates the socketio test client, the errors does not appear. This is quite strange because I have no idea why the creation of socketio test client can lead to detached instances.

@miguelgrinberg
Copy link
Owner Author

@LostInDarkMath Let me know if this fix addresses the problem for you.

@LostInDarkMath
Copy link

Thanks Miguel! This works perfectly fine :)

@LostInDarkMath
Copy link

When will you release this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants