python manage.py migrate error when using wamp mysql

Hi, I am building a project to learn about Django. I had connected the database with my project.
I did makemigrations and migrate after i added tables to models.py

python manage.py makemigrations
No changes detected
python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/4.1/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: CareCollective, admin, auth, contenttypes, sessions
Running migrations:
  Applying CareCollective.0001_initial...Traceback (most recent call last):  .......

The link in the warning message describes the issue the possible solutions.

Hi I don’t understand the error. Would you be able to help me.
I checked the mysql database the table users don’t exist.

python manage.py makemigrations
No changes detected
PS D:\mini project 2024\DonateItAll> python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
       HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/4.1/ref/databases/#mysql-sql-mode
Operations to perform:
 Apply all migrations: CareCollective, admin, auth, contenttypes, sessions
Running migrations:
 Applying CareCollective.0001_initial...Traceback (most recent call last):
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 87, in _execute
   return self.cursor.execute(sql)
          ^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
   return self.cursor.execute(query, args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
   res = self._query(mogrified_query)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\.....\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
   db.query(q)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py", line 255, in query
   _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1050, "Table 'users' already exists")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "D:\mini project 2024\DonateItAll\manage.py", line 22, in <module>
   main()
 File "D:\mini project 2024\DonateItAll\manage.py", line 18, in main
   execute_from_command_line(sys.argv)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
   utility.execute()
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 440, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
   self.execute(*args, **cmd_options)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 448, in execute
   output = self.handle(*args, **options)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 96, in wrapped
   res = handle_func(*args, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\migrate.py", line 349, in handle
   post_migrate_state = executor.migrate(
                        ^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 135, in migrate
   state = self._migrate_all_forwards(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 167, in _migrate_all_forwards
   state = self.apply_migration(
           ^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 252, in apply_migration
   state = migration.apply(state, schema_editor)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\migration.py", line 130, in apply
   operation.database_forwards(
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\operations\models.py", line 96, in database_forwards
   schema_editor.create_model(model)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\base\schema.py", line 447, in create_model
   self.execute(sql, params or None)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\base\schema.py", line 199, in execute
   cursor.execute(sql, params)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 102, in execute
   return super().execute(sql, params)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
   return self._execute_with_wrappers(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
   return executor(sql, params, many, context)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
   with self.db.wrap_database_errors:
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 91, in __exit__
   raise dj_exc_value.with_traceback(traceback) from exc_value
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 87, in _execute
   return self.cursor.execute(sql)
          ^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
   return self.cursor.execute(query, args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
   res = self._query(mogrified_query)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
   db.query(q)
 File "C:\Users\....\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py", line 255, in query
   _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1050, "Table 'users' already exists")
PS D:\mini project 2024\DonateItAll>

Ok, now I can see the error. (I thought you were asking about that warning message since that’s all you posted above.)

This looks like a situation where your migration files have gotten out-of-sync with your database.

Have you at any point deleted any migration files? If so, your easiest solution is going to be to drop the database and recreate it. (Yes, it’s possible to recover from this, but it can be a fair amount of work to do so. The easiest solution is to just recreate th database.)

If you have never deleted any migration files in this project, then we would need to dig further into the cause. Is it possible that this database was used in a different project?

I have not deleted the migrations. When i first added my data to models.py and made my database as mysql this error started showing. Makemigrations works properly just migrate shows this error. Like you had sugessted i dropped the database and created it again. Still the error persists.
The database was not used in any other project.

Please post your models.py file, along with the output of a manage.py showmigrations command.

models.py

from django.db import models
from django.utils.timezone import now

#
class Users_Model(models.Model):
    UID = models.AutoField(primary_key=True)  # Use AutoField for auto-incrementing
    Name = models.CharField(max_length=100)
    Mobile_Number = models.CharField(max_length=10)  # Use CharField for phone numbers
    Email_Id = models.EmailField(max_length=100)    # Use EmailField for email
    Date_Of_Join = models.DateField(default=now)
    Admin_Flag = models.CharField(max_length=20,default="False")  # Use BooleanField for flags
    Status = models.CharField(max_length=10, default="Active")

    class Meta:
        db_table = "users"

class Login_Model(models.Model):
    Login_ID = models.AutoField(primary_key=True)  # Use AutoField for auto-incrementing
    UID = models.ForeignKey(Users_Model, on_delete=models.CASCADE,db_column='UID')  # ForeignKey with cascade delete
    Username = models.CharField(max_length=100, unique=True)  # Ensure uniqueness
    Password = models.CharField(max_length=100)

    class Meta:
        db_table = "login"
        
        
        
class Donor_Model(models.Model):
    Donor_Id = models.AutoField(primary_key=True)
    UID = models.ForeignKey(Users_Model, on_delete=models.CASCADE,db_column='UID')  # ForeignKey with cascade delete
    Donation_History_Count = models.IntegerField(default=0) 
    Verified_Date  = models.DateField(default=now)
    Last_Donation_Date = models.DateField(null=True, blank=True)
    Preferred_Donation_Categories= models.CharField(max_length=200, null=True, blank=True)

    class Meta:
        db_table = "donor"

makemigrations

python manage.py makemigrations
No changes detected

Please post the output of a manage.py showmigrations command, not a makemigrations.

Sure

python manage.py showmigrations
CareCollective
 [ ] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
PS D:\mini project 2024\DonateItAll> python manage.py makemigrations
No changes detected
PS D:\mini project 2024\DonateItAll> python manage.py showmigrations
CareCollective
 [ ] 0001_initial
 [ ] 0002_alter_users_model_table
 [ ] 0003_alter_users_model_table
 [ ] 0004_alter_users_model_admin_flag
 [ ] 0005_alter_login_model_uid
 [ ] 0006_donor_model
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial

This is what is shown for showmigrations.

This is the issue:

MySQL, by default creates a table by that name. If you are intending to use that default table, you need to indicate that the table already exists by using the managed = False setting.
If the table you are looking to use has nothing to do with that default table, change the name or allow Django to use its default name for that model.
(My general suggestion is to always allow Django to use its names for tables, but there are situations where you might not want to do that.)

I had changed the name of the table users to main_users and did makemigrations and then migrate still the error is shown as “users” table already exists

If a showmigrations doesn’t show any of your app’s migrations as having been applied, delete the migrations from your app’s migrations directory, do another makemigrations and apply them.

I deleted the migrations and did makemigrations and then migrate. I renamed the table as main_users which shows that main_users already exists.

Starting with a clean database, please post the actual commands that you are issuing, in the order that they’re being issued, along with the output generated by those commands.