@@ -25,30 +25,18 @@ def handle(self, *args, **options):
2525 self .stdout .write (f"User model: { User } " )
2626 self .stdout .write (f"DB table: { User ._meta .db_table } " )
2727
28- # Debug: check if the table exists
28+ # Debug: check if the table exists (backend-agnostic)
29+ table_names = set (connection .introspection .table_names ())
30+ if User ._meta .db_table not in table_names :
31+ self .stderr .write (self .style .ERROR (f"Table '{ User ._meta .db_table } ' does NOT exist!" ))
32+ self .stderr .write ("Migration likely failed. Check 'python manage.py showmigrations'" )
33+ return
34+
35+ self .stdout .write (f"Table '{ User ._meta .db_table } ' exists" )
2936 with connection .cursor () as cursor :
30- cursor .execute (
31- "SELECT tablename FROM pg_tables WHERE tablename = %s" ,
32- [User ._meta .db_table ],
33- )
34- row = cursor .fetchone ()
35- if row :
36- self .stdout .write (f"Table '{ User ._meta .db_table } ' exists" )
37- # Check columns
38- cursor .execute (
39- "SELECT column_name FROM information_schema.columns WHERE table_name = %s ORDER BY ordinal_position" ,
40- [User ._meta .db_table ],
41- )
42- cols = [r [0 ] for r in cursor .fetchall ()]
43- self .stdout .write (f"Columns: { ', ' .join (cols )} " )
44- # Count existing users
45- cursor .execute (f"SELECT COUNT(*) FROM { User ._meta .db_table } " )
46- count = cursor .fetchone ()[0 ]
47- self .stdout .write (f"Existing users: { count } " )
48- else :
49- self .stderr .write (self .style .ERROR (f"Table '{ User ._meta .db_table } ' does NOT exist!" ))
50- self .stderr .write ("Migration likely failed. Check 'python manage.py showmigrations'" )
51- return
37+ columns = connection .introspection .get_table_description (cursor , User ._meta .db_table )
38+ self .stdout .write (f"Columns: { ', ' .join (col .name for col in columns )} " )
39+ self .stdout .write (f"Existing users: { User .objects .count ()} " )
5240
5341 # Debug: check migration state
5442 with connection .cursor () as cursor :
0 commit comments