1

I am using Django3 and Python3 with the latest version to learn Django.

I want to use an existing sqlite3 database for my django project. But I have problems while I was using the migrate command.

I have done very few steps to my project so far:

1. use the venv, upgrade pip, install django,

2. generate a project,

3. insert an app, and add it into INSTALLED_APPS,

4. In the settings.py, I added the existing database into the DATABASES as follows.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },

    'testdb': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'TestDB.sqlite3'),
    },
}
  1. Then I use the method as here shows, to generate the models.py file. Using existing database in Django

The generated models.py looks like: (where I added the max_length=25)

from django.db import models


class Stars(models.Model):
    actorname = models.CharField(db_column='ActorName', blank=True, null=True, max_length=25)
    realname = models.CharField(db_column='RealName', blank=True, null=True, max_length=25)

    class Meta:
        managed = False
        db_table = 'Stars'

6. Then I run python manage.py migrate and/or python manage.py makemigrations, here I see the following errors:

    (venv) PS D:\Workfolder_Web\testProject\backend\project> python .\manage.py migrate
    Traceback (most recent call last):
    File ".\manage.py", line 21, in <module>
        main()
    File ".\manage.py", line 17, in main
        execute_from_command_line(sys.argv)
    File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line 
        utility.execute()
    File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
        django.setup()
    File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
    File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
        app_config.import_models()
    File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\apps\config.py", line 211, in import_models
        self.models_module = import_module(models_module_name)
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
    File "<frozen importlib._bootstrap>", line 991, in _find_and_load
    File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 779, in exec_module
    File "<frozen importlib._bootstrap_external>", line 916, in get_code
    File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    ValueError: source code string cannot contain null bytes

7. My database TestDB.sqlite3 is very simple, it looks like: enter image description here

Question: How could I debug it? I am not using any bootstrap, urls, views or routers in my current codes.

Thanks for the help!

1 Answer 1

0

If you changed the Models you either add a default value for the new fields or remove the database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.