datatype mismatch when i try to add to a table

i’m trying to add a new skill to my table (skills) but it refuses and shows me an exception (datatype mismatch) i did all of the migrations
here is the model
class Skill(models.Model):
owner = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=200, blank=True, null=True)
description = models.TextField(null=True, blank=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__(self):
    return str(self.name)

Please post the complete error message with the traceback in addition to the code where the error is occurring.

Also, when you’re posting code here, surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

traceback:
Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/users/skill/add/

Django Version: 4.2
Python Version: 3.10.10
Installed Applications:
[‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘projects.apps.ProjectsConfig’,
‘users.apps.UsersConfig’]
Installed Middleware:
[‘django.middleware.security.SecurityMiddleware’,
‘whitenoise.middleware.WhiteNoiseMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’]

Traceback (most recent call last):
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\utils.py”, line 89, in _execute
return self.cursor.execute(sql, params)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\sqlite3\base.py”, line 328, in execute
return super().execute(query, params)

The above exception (datatype mismatch) was the direct cause of the following exception:
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\contrib\admin\options.py”, line 688, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\utils\decorators.py”, line 134, in _wrapper_view
response = view_func(request, *args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\views\decorators\cache.py”, line 62, in _wrapper_view_func
response = view_func(request, *args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\contrib\admin\sites.py”, line 242, in inner
return view(request, *args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\contrib\admin\options.py”, line 1886, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\utils\decorators.py”, line 46, in _wrapper
return bound_method(*args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\utils\decorators.py”, line 134, in _wrapper_view
response = view_func(request, *args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\contrib\admin\options.py”, line 1747, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\contrib\admin\options.py”, line 1798, in _changeform_view
self.save_model(request, new_object, form, not add)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\contrib\admin\options.py”, line 1227, in save_model
obj.save()
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\base.py”, line 814, in save
self.save_base(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\base.py”, line 877, in save_base
updated = self._save_table(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\base.py”, line 1020, in _save_table
results = self._do_insert(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\base.py”, line 1061, in _do_insert
return manager._insert(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\manager.py”, line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\query.py”, line 1805, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\sql\compiler.py”, line 1820, in execute_sql
cursor.execute(sql, params)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\utils.py”, line 102, in execute
return super().execute(sql, params)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\utils.py”, line 67, in execute
return self._execute_with_wrappers(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\utils.py”, line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\utils.py”, line 84, in _execute
with self.db.wrap_database_errors:
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\utils.py”, line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\utils.py”, line 89, in _execute
return self.cursor.execute(sql, params)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\sqlite3\base.py”, line 328, in execute
return super().execute(query, params)

Exception Type: IntegrityError at /admin/users/skill/add/
Exception Value: datatype mismatch

i’m not sure what part of my code is causing this error as it’s not clear from the exception

So the error is happening in the Admin.

What does your admin class look like for this model (Skill)?


this is my admin page
i made sure of the spelling so i don’t think that’s the issue

Please show the schema of the Skill table in the database.

Also, please do not post images of code, errors, tracebacks, etc.
When you’re posting code here, surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

    owner = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, blank=True, null=True)
    description = models.TextField(null=True, blank=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return str(self.name)```

That’s your model - you posted them earlier.

I’m asking for you to examine the database itself and report on the schema as it exists in the database.

i can only access the database from the admin

In that case then my only suggestion is for you to change your reference to a different database file, do your initial migrations, and try it again.

(And, you should find a tool allowing you to access the database directly.)

1 Like

i installed Pycharm and found this for skill
operations = [
migrations.CreateModel(
name=‘skill’,
fields=[
(‘name’, models.CharField(blank=True, max_length=200, null=True)),
(‘description’, models.TextField(blank=True, null=True)),
(‘id’, models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
(‘owner’, models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=‘users.profile’)),
],
),
]

That is still looking at your Python code and not the database.

If you’re working with sqlite (I think I saw a reference to that in the traceback), the command-line command to directly access a sqlite database is sqlite3. (This provides similar functionality to the psql command for PostgreSQL and mysql for MySQL/MariaDB.)

Note, on Windows, you may need to download the sqlite bundle for windows from the SQLite Download Page (see the section for the precompiled binaries for Windows)

i did this and i could’nt access any table, i think something is wrong with the database.
i tried to run the migrations again but it did’t work

Please post here (as text, not screen images) the configuration change(s) you made, the complete commands that you ran, and all the output from those commands.

In app project

‘’'from django.db import models
import uuid
from users.models import Profile

Create your models here.

class Project(models.Model):
owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank = True)
featured_image = models.ImageField(null=True, blank = True, default=‘default.jpg’)
demo_link = models.CharField(max_length=2000, null=True, blank = True)
source_link = models.CharField(max_length=2000, null=True, blank = True)
tags = models.ManyToManyField(‘Tag’, blank = True)
vote_total = models.IntegerField(default=0, null= True, blank= True)
vote_ratio = models.IntegerField(default=0, null= True, blank= True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__(self):
    return self.title

#one to many relation
class Review(models.Model):
VOTE_TUPE = (
(‘up’, ‘Up Vote’),
(‘down’, ‘Down Vote’),
)
#owner =
project = models.ForeignKey(Project, on_delete=models.CASCADE)
body = models.TextField(null=True, blank = True)
value = models.CharField(max_length=200, choices=VOTE_TUPE)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__(self):
    return self.value

#many to many relation
class Tag(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__(self):
    return self.name'''

commands: makemigrations & migrate

In app users:
‘’'from django.db import models
import uuid
from django.contrib.auth.models import User

Create your models here.

#we want to extend the user model that django built so we will create on to one relation
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=200, blank=True, null=True)
email = models.CharField(max_length=500, blank=True, null=True)
username = models.CharField(max_length=200, blank=True, null=True)
short_intro = models.CharField(max_length=200, blank=True, null=True)
bio = models.TextField(blank=True, null=True)
profile_image = models.ImageField(null=True, blank=True, upload_to=‘profiles/’, default=‘profiles/user-default.png’)
social_github = models.CharField(max_length=200, blank=True, null=True)
social_twitter = models.CharField(max_length=200, blank=True, null=True)
social_linkedin = models.CharField(max_length=200, blank=True, null=True)
social_youtube = models.CharField(max_length=200, blank=True, null=True)
social_website = models.CharField(max_length=200, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__(self):
    return str(self.username)

class Skill(models.Model):
owner = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=200, blank=True, null=True)
description = models.TextField(null=True, blank=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

def __str__(self):
    return str(self.name)'''

commands: makemigrations & migrate

i can’t post the outputs but i didn’t encounter any error

Reminder: When you’re posting code here, surround the code between lines of three backtick - ` characters. This means you’ll have a line of , then your code, then another line of .

Those lines of ``` must be lines by themselves. They must not be part (beginning or end) of other lines.

What is the output of a manage.py showmigrations command?

Why not?

this is the output of showmigrations:
admin
0001_initial
0002_logentry_remove_auto_add
0003_logentry_add_action_flag_choices
auth
0001_initial
0002_alter_permission_name_max_length
0003_alter_user_email_max_length
0004_alter_user_username_opts
0005_alter_user_last_login_null
0006_require_contenttypes_0002
0007_alter_validators_add_error_messages
0008_alter_user_username_max_length
0009_alter_user_last_name_max_length
0010_alter_group_name_max_length
0011_update_proxy_permissions
0012_alter_user_first_name_max_length
contenttypes
0001_initial
0002_remove_content_type_name
projects
0001_initial
0002_tag_project_vote_ratio_project_vote_total_review_and_more
0003_project_featured_image
0004_project_owner
sessions
0001_initial
users
0001_initial
0002_profile_username
0003_skill
0004_profile_location_skill_created_alter_skill_id
0005_remove_skill_created
0006_remove_profile_location
0007_profile_location
0008_remove_profile_location
0009_delete_skill
0010_skill
0011_remove_skill_owner_profile_skills
0012_remove_profile_skills_skill_owner
0013_profile_location
0014_remove_profile_location
0015_profile_location
0016_remove_profile_location

You have a number of migrations that have not been applied.

What is your output from a manage.py migrate command?

this is the output of migrate:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, projects, sessions, users
Running migrations:
Applying users.0004_profile_location_skill_created_alter_skill_id…Traceback (most recent call last):
File “C:\Users\Athee\Downloads\devsearch\manage.py”, line 22, in
main()
File “C:\Users\Athee\Downloads\devsearch\manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\management_init_.py”, line 442, in execute_from_command_line
utility.execute()
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\management_init_.py”, line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\management\base.py”, line 412, in run_from_argv
self.execute(*args, **cmd_options)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\management\base.py”, line 458, in execute
output = self.handle(*args, **options)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\management\base.py”, line 106, in wrapper
res = handle_func(*args, **kwargs)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\core\management\commands\migrate.py”, line 356, in handle
post_migrate_state = executor.migrate(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\migrations\executor.py”, line 135, in migrate
state = self.migrate_all_forwards(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\migrations\executor.py”, line 167, in migrate_all_forwards
state = self.apply_migration(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\migrations\executor.py”, line 252, in apply_migration
state = migration.apply(state, schema_editor)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\migrations\migration.py”, line 132, in apply
operation.database_forwards(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\migrations\operations\fields.py”, line 108, in database_forwards
schema_editor.add_field(
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\sqlite3\schema.py”, line 398, in add_field
self.remake_table(model, create_field=field)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\sqlite3\schema.py”, line 238, in remake_table
self.effective_default(create_field),
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\backends\base\schema.py”, line 429, in effective_default
return field.get_db_prep_save(self.effective_default(field), self.connection)
File "C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\fields_init
.py", line 954, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\fields_init
.py", line 1607, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\fields_init
.py", line 1585, in get_prep_value
value = super().get_prep_value(value)
File "C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\fields_init
.py", line 1464, in get_prep_value
return self.to_python(value)
File "C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\db\models\fields_init
.py", line 1546, in to_python
parsed = parse_datetime(value)
File “C:\Users\Athee\Downloads\devsearch\env\lib\site-packages\django\utils\dateparse.py”, line 114, in parse_datetime
return datetime.datetime.fromisoformat(value)
TypeError: fromisoformat: argument must be str