0

I connected my Django with mysql database and ran a sql script to make a database and ran migrations on my django project and I had no errors but to to use the database I had to use python manage.py inspectdb >appname/models.py , after doing this I cleaned my models as it was mentioned in the steps but when I ran python manage.py migrate it gave me this error:

MYSQL script

create database if not exists payroll_db;
use payroll_db;
create table m_state(state_code varchar(2) primary key, state_name varchar(30));

create table m_address(address_id integer primary key,building_details varchar(30) NOT NULL, road varchar(20) NOT NULL,landmark varchar(30) NOT NULL, city varchar(30) NOT NULL,state varchar(2) NOT NULL references m_state(state_code),country varchar(30) default 'India');

create table m_company(company_id integer primary key, company_name varchar(50) NOT NULL, building_details varchar(30) NOT NULL, Area Varchar(30),  Landmark Varchar(30), City varchar(30), state varchar(2) NOT NULL references m_state(state_code),country varchar(30) default 'India');

create table m_department(company_id integer NOT NULL references m_company(company_id),department_id integer primary key,department_name varchar(30) NOT NULL);

create table m_grade(grade_id integer primary key, grade_name varchar(20) NOT NULL);

create table m_employee(employee_id integer primary key,employee_name varchar(30) NOT NULL,department_id integer references m_department(department_id),company_id integer NOT NULL references m_company(company_id), building_details varchar(30) NOT NULL, Area Varchar(30),  Landmark Varchar(30), City varchar(30), state varchar(2) NOT NULL references m_state(state_code),country varchar(30) default 'India',  employee_doj date NOT NULL, grade_id integer NOT NULL references m_grade (grade_id));

create table m_paygrade(employee_id integer NOT NULL references m_employee(employee_id) ,grade_id integer NOT NULL references m_grade(grade_id),basic_amt integer NOT NULL,da_amt integer NOT NULL,pf_amt integer NOT NULL,medical_amt integer NOT NULL,primary key(employee_id,grade_id));

create table m_pay(employee_id integer NOT NULL references m_employee(employee_id),fin_year integer NOT NULL, gross_salary integer NOT NULL,gross_dedn integer NOT NULL,net_salary integer NOT NULL, primary key(fin_year, employee_id));

create table t_leave( employee_id integer NOT NULL references m_employee(employee_id) ,fin_year integer NOT NULL,leave_date date NOT NULL, leave_type varchar(4) check (leave_type IN('CL','SL','PL','LWP')));

create table t_achievement(employee_id integer NOT NULL references m_employee(employee_id),achievement_date date NOT NULL, achievement_type varchar(80) NOT NULL);

django models.py code:

from django.db import models
# class AuthGroup(models.Model):
#     name = models.CharField(unique=True, max_length=150)

#     class Meta:
#         managed = True
#         db_table = 'auth_group'


# class AuthGroupPermissions(models.Model):
#     group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
#     permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)

#     class Meta:
#         managed = True
#         db_table = 'auth_group_permissions'
#         unique_together = (('group', 'permission'),)


# class AuthPermission(models.Model):
#     name = models.CharField(max_length=255)
#     content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
#     codename = models.CharField(max_length=100)

#     class Meta:
#         managed = True
#         db_table = 'auth_permission'
#         unique_together = (('content_type', 'codename'),)


# class AuthUser(models.Model):
#     password = models.CharField(max_length=128)
#     last_login = models.DateTimeField(blank=True, null=True)
#     is_superuser = models.IntegerField()
#     username = models.CharField(unique=True, max_length=150)
#     first_name = models.CharField(max_length=150)
#     last_name = models.CharField(max_length=150)
#     email = models.CharField(max_length=254)
#     is_staff = models.IntegerField()
#     is_active = models.IntegerField()
#     date_joined = models.DateTimeField()

#     class Meta:
#         managed = True
#         db_table = 'auth_user'


# class AuthUserGroups(models.Model):
#     user = models.ForeignKey(AuthUser, models.DO_NOTHING)
#     group = models.ForeignKey(AuthGroup, models.DO_NOTHING)

#     class Meta:
#         managed = True
#         db_table = 'auth_user_groups'
#         unique_together = (('user', 'group'),)


# class AuthUserUserPermissions(models.Model):
#     user = models.ForeignKey(AuthUser, models.DO_NOTHING)
#     permission = models.ForeignKey(AuthPermission, models.DO_NOTHING)

#     class Meta:
#         managed = True
#         db_table = 'auth_user_user_permissions'
#         unique_together = (('user', 'permission'),)


# class DjangoAdminLog(models.Model):
#     action_time = models.DateTimeField()
#     object_id = models.TextField(blank=True, null=True)
#     object_repr = models.CharField(max_length=200)
#     action_flag = models.PositiveSmallIntegerField()
#     change_message = models.TextField()
#     content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING, blank=True, null=True)
#     user = models.ForeignKey(AuthUser, models.DO_NOTHING)

#     class Meta:
#         managed = True
#         db_table = 'django_admin_log'


# class DjangoContentType(models.Model):
#     app_label = models.CharField(max_length=100)
#     model = models.CharField(max_length=100)

#     class Meta:
#         managed = True
#         db_table = 'django_content_type'
#         unique_together = (('app_label', 'model'),)


# class DjangoMigrations(models.Model):
#     app = models.CharField(max_length=255)
#     name = models.CharField(max_length=255)
#     applied = models.DateTimeField()

#     class Meta:
#         managed = True
#         db_table = 'django_migrations'


# class DjangoSession(models.Model):
#     session_key = models.CharField(primary_key=True, max_length=40)
#     session_data = models.TextField()
#     expire_date = models.DateTimeField()

#     class Meta:
#         managed = True
#         db_table = 'django_session'

class MState(models.Model):
    state_code = models.CharField(primary_key=True, max_length=2)
    state_name = models.CharField(max_length=30, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'm_state'


class MAddress(models.Model):
    address_id = models.IntegerField(primary_key=True)
    building_details = models.CharField(max_length=30)
    road = models.CharField(max_length=20)
    landmark = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    state = models.ForeignKey('Mstate', on_delete=models.CASCADE)
    country = models.CharField(max_length=30, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'm_address'


class MCompany(models.Model):
    company_id = models.IntegerField(primary_key=True)
    company_name = models.CharField(max_length=50)
    building_details = models.CharField(max_length=30)
    area = models.CharField(db_column='Area', max_length=30, blank=True, null=True)  # Field name made lowercase.
    landmark = models.CharField(db_column='Landmark', max_length=30, blank=True, null=True)  # Field name made lowercase.
    city = models.CharField(db_column='City', max_length=30, blank=True, null=True)  # Field name made lowercase.
    state = models.ForeignKey('Mstate', on_delete=models.CASCADE)
    country = models.CharField(max_length=30, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'm_company'


class MDepartment(models.Model):
    company_id = models.ForeignKey('MCompany',on_delete=models.CASCADE)
    department_id = models.IntegerField(primary_key=True)
    department_name = models.CharField(max_length=30)

    class Meta:
        managed = True
        db_table = 'm_department'


class MGrade(models.Model):
    grade_id = models.IntegerField(primary_key=True)
    grade_name = models.CharField(max_length=20)

    class Meta:
        managed = True
        db_table = 'm_grade'


class MEmployee(models.Model):
    employee_id = models.IntegerField(primary_key=True)
    employee_name = models.CharField(max_length=30)
    department_id = models.ForeignKey('MDepartment',on_delete=models.CASCADE)
    company_id = models.ForeignKey('MCompany',on_delete=models.CASCADE)
    building_details = models.CharField(max_length=30)
    area = models.CharField(db_column='Area', max_length=30, blank=True, null=True)  # Field name made lowercase.
    landmark = models.CharField(db_column='Landmark', max_length=30, blank=True, null=True)  # Field name made lowercase.
    city = models.CharField(db_column='City', max_length=30, blank=True, null=True)  # Field name made lowercase.
    state = models.ForeignKey('Mstate', on_delete=models.CASCADE)
    country = models.CharField(max_length=30, blank=True, null=True)
    employee_doj = models.DateField()
    grade_id = models.ForeignKey('MGrade',on_delete=models.CASCADE)

    class Meta:
        managed = True
        db_table = 'm_employee'

class MPaygrade(models.Model):
    employee_id = models.ForeignKey('MEmployee',on_delete=models.CASCADE)
    grade_id = models.ForeignKey('MGrade',on_delete=models.CASCADE)
    basic_amt = models.IntegerField()
    da_amt = models.IntegerField()
    pf_amt = models.IntegerField()
    medical_amt = models.IntegerField()
    paygrade_id = models.AutoField(primary_key=True)
    class Meta:
        managed = True
        db_table = 'm_paygrade'
        unique_together = (('employee_id', 'grade_id'),)


class MPay(models.Model):
    employee_id = models.ForeignKey('MEmployee',on_delete=models.CASCADE)
    fin_year = models.IntegerField()
    gross_salary = models.IntegerField()
    gross_dedn = models.IntegerField()
    net_salary = models.IntegerField()
    MPay_id = models.AutoField(primary_key=True)
    class Meta:
        managed = True
        db_table = 'm_pay'
        unique_together = (('fin_year', 'employee_id'),)

class TLeave(models.Model):
    employee_id = models.ForeignKey('MEmployee',on_delete=models.CASCADE)
    fin_year = models.IntegerField()
    leave_date = models.DateField()
    leave_type = models.CharField(max_length=4, blank=True, null=True)
    leave_id =models.AutoField(primary_key=True)

    class Meta:
        managed = True
        db_table = 't_leave'


class TAchievement(models.Model):
    employee_id = models.ForeignKey('MEmployee',on_delete=models.CASCADE)
    achievement_date = models.DateField()
    achievement_type = models.CharField(max_length=80)
    achievement_id = models.AutoField(primary_key=True)

    class Meta:
        managed = True
        db_table = 't_achievement'


error traceback:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\chira\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\chira\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\chira\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\chira\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\chira\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\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

1 Answer 1

1

I just had this error, the output file has null bytes \0x00 that u need to inspect and remove manually with python.

u may try something like this

content = []
with open('crm/models.py', 'r') as file_in:
    content = file_in.readlines()

new_content = [] 
for line in content:
    clean_line = line.replace('\00', '')
    new_content.append(clean_line)

with open('models2.py', 'w') as file_out:
    for line in new_content:
        file_out.write(line)

or print every line and copy and paste also worked

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.