Skip to content

Live server error with sqlite3 #783

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

Open
cleder opened this issue Nov 18, 2019 · 10 comments
Open

Live server error with sqlite3 #783

cleder opened this issue Nov 18, 2019 · 10 comments

Comments

@cleder
Copy link

cleder commented Nov 18, 2019

When using the live_server in an integration test fixture:

@pytest.fixture(scope='session', name='server_url')
def get_server_url(live_server, environment):
    """Start a django server to test against and return the url of the server."""
    yield live_server.url

the following error is generated:


self = <[AttributeError("'LiveServer' object has no attribute 'thread'") raised in repr()] LiveServer object at 0x80526a8d0>
addr = 'localhost'

    def __init__(self, addr):
        import django
        from django.db import connections
        from django.test.testcases import LiveServerThread
        from django.test.utils import modify_settings
    
        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (
                conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3"
                and conn.settings_dict["NAME"] == ":memory:"
            ):
                # Explicitly enable thread-shareability for this connection
>               conn.allow_thread_sharing = True
E               AttributeError: can't set attribute

/home/goldeneagle/venv/lib/python3.7/site-packages/pytest_django/live_server_helper.py:27: AttributeError
@cleder
Copy link
Author

cleder commented Nov 18, 2019

This may be hard to reproduce
Environment: Python 3.7.5 on FreeBSD with py37-sqlite
with pytest 4.5.0 and 5.2.4
Django 2.2.7

@blueyed
Copy link
Contributor

blueyed commented Nov 18, 2019

Needs to be adjusted for Django 2.2+ I think, ref: django/django@76990cb.

@blueyed
Copy link
Contributor

blueyed commented Jan 5, 2020

I wonder why you hit the (apparently wrong) conn.settings_dict["NAME"] == ":memory:".. (this should check the TEST db name). Are you using an in-memory DB by default within your tests?

@blueyed
Copy link
Contributor

blueyed commented Jan 5, 2020

See #793 for a fix.

@cleder cleder closed this as completed Jan 6, 2020
@blueyed
Copy link
Contributor

blueyed commented Jan 6, 2020

@cleder
Have you tried #793?

@cleder
Copy link
Author

cleder commented Jan 7, 2020

Yes switching to this branch fixed it for me

blueyed added a commit to blueyed/pytest-django that referenced this issue Mar 31, 2020
@dekoza
Copy link

dekoza commented Apr 29, 2020

I encountered this behavior in one of my projects. Interestingly, I have another project with same configs - and in that one live_server works.

@bluetech
Copy link
Member

Looks like the commit hasn't been merged so this should still be open.

@bluetech bluetech reopened this Oct 17, 2020
@boxed
Copy link
Contributor

boxed commented Feb 19, 2021

I hit this problem today. Can I help get this sorted?

@alorence
Copy link

alorence commented Nov 12, 2021

As a workaround, simply set your database name to file::memory:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': "file::memory:",
    },
}

The problem came from live_server_helper.py, in the LiveServer constructor:

        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (
                conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3"
                and conn.settings_dict["NAME"] == ":memory:"
            ):
                # Explicitly enable thread-shareability for this connection
               conn.allow_thread_sharing = True

As conn.settings_dict["NAME"] is compared to str :memory:, changing that value to something else will prevent from entering the if-block. So, the wrong statement wont be executed.

Since Django 2.2, allow_code_sharing is a read-only property of BaseDatabaseWrapper object (see https://github.com/django/django/blob/stable/2.2.x/django/db/backends/base/base.py#L520). It was previously a normal member (see https://github.com/django/django/blob/stable/2.1.x/django/db/backends/base/base.py#L83).

Hope one of the open pull request regarding this issue will be merged soon.

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