找回密码
 注册
查看: 691|回复: 4

一次python django项目数据同步异常操作,重置过程,并同步到数据库

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-7-18 17:17:02 | 显示全部楼层 |阅读模式
因调试过程中,发现有点问题,就把数据库表从其他客户端删除了,以为删除了就可以同步过去,没想到就是下面的结果。
. I& ]- y/ H# L(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py check4 d& l3 T( Y) }, _- ^8 [: n& g/ P
System check identified no issues (0 silenced).
4 [9 n, X- v! {- o6 K- {& o; w, b
$ I$ s# H: N8 g4 C4 G2 k# l# ](python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations
& {$ H3 C. x' W( y) f+ Y6 m- gNo changes detected' m4 R! `  Q# k* A3 M+ G: b

9 ?0 O2 l& B9 V) a% V/ X& w/ Q( ^% N8 m(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations order, Y* ~( w5 ?& j' p# J
No changes detected in app 'order'
9 x# j+ p$ X- z9 \# l, j( u, `3 q! v% \. S) g, ~. m  _- I
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order( C% ~) v' \9 _9 F6 O: N
Operations to perform:
( T5 G$ D: S; A( }& v2 G  Apply all migrations: order4 F8 @/ l3 u1 @4 K4 f
Running migrations:
* `5 X5 N' ?7 a5 b3 z  No migrations to apply.
; R  z1 l7 M# ?. [6 t
8 s9 e' v  F6 M% Z: M1 J3 A同步不过去,怎么办?
! {$ i* ~1 Q3 g- g+ w' ]3 k" r看到有高手的同步经历,按照他的方法,去执行了下,好像问题解决了。5 Y3 N" }: Y' i) E
' @( z9 y5 ^1 X/ Q( ?; Q
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell1 x, a$ i% S+ ^( @& R9 j4 R
Welcome to the MariaDB monitor.  Commands end with ; or \g.3 _1 c! x2 g$ |# c  A1 V0 b1 |. Y
Your MariaDB connection id is 152
2 K9 r, B- U/ E! M& A3 F& ZServer version: 11.5.1-MariaDB mariadb.org binary distribution
5 z0 r# }  c' I6 i% B; _2 ~
; s' W& N- X+ S4 I$ e: z8 dCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.: L! b" l* q9 X! L+ i
8 l+ d, w# ?' y1 P
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.8 w$ E7 y( U) o1 m- A

. \1 S1 P# K' B& }9 h  v/ kMariaDB [tripsql]> show tables;: @+ ]3 n% G# K: a
+-------------------------------+
, U5 a# l1 i; n" B| Tables_in_tripsql             |  U. O+ J4 P7 r, R$ F
+-------------------------------+/ V9 \, [7 Q6 P6 @
| account_user                  |! H2 c! _3 U4 l( I$ |$ D
| account_user_groups           |
8 D, b- L1 V% c3 @9 U% f: ]| account_user_user_permissions |/ m4 h9 K6 x# i) I& A
| accounts_login_record         |( |! C, v$ x& u5 Y2 s) A! F
| accounts_user_profile         |
# ]; E: j3 l2 X3 }| auth_group                    |
. B7 j! k# u, H( E* d2 y| auth_group_permissions        |
- v9 q# Y% U9 x| auth_permission               |- A5 X/ m. B* f0 O
| auth_user                     |
% z: X: i* }( ]% Q# w( S( _| auth_user_groups              |$ T0 r" L2 ]  N% J
| auth_user_user_permissions    |
2 c. D  F( O7 @) J* l| django_admin_log              |
+ Y- `2 B4 [/ C| django_content_type           |" ?2 _; M" q7 \0 a7 i* k0 H9 e
| django_migrations             |
2 ~5 q3 H* D& L. a| django_session                |- k7 Y- O& n. i. }
| sight                         |2 n1 `+ d& r. p" Q
| sight_comment                 |- H$ s2 O+ {" [, \- e2 S7 Q
| sight_info                    |
8 G2 J7 x. ]3 {) K6 v| sight_ticket                  |
& Z3 k: c) I& o. ^/ R5 p& e3 D| system_image_related          |
" E6 n1 k, j; U' p- J9 Q| system_slider                 |( w% E5 ~' c& ]! }$ a  q
+-------------------------------+' h5 ^( ^3 Q% ^4 P2 X& `/ K
21 rows in set (0.002 sec)
% ]+ W& [! R. n
- Y; N( v0 `, r$ {1 r- |& i, o. v. w* ~; s1 d  |
MariaDB [tripsql]> delete from django_migrations where app='order';
7 `# {' p* K  n4 h8 k1 f0 a- NQuery OK, 2 rows affected (0.004 sec)
- [( B7 c( s' f  w' \8 i5 X
6 M/ R9 Y: D3 @+ p) CMariaDB [tripsql]> exit% C3 T/ y7 B% d. R( w
Bye% r" \# x# b' y

$ m+ X, p! j/ m+ s; r) }0 b(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order( D" V% J3 m" S% `0 V; q
Operations to perform:6 Z  D- }; g& W( H" o
  Apply all migrations: order
4 B( ^* _. [6 B# C; c+ L9 hRunning migrations:! C# p9 }6 {1 ?& M. `
  Applying order.0001_initial... OK. S4 W4 Q5 T5 Q! U

1 S. ~5 g" F/ ]4 S(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>
0 u/ i- p' Z9 r7 ^+ u* A' l0 Z这次成功同步过去了。
. E. |$ l0 K) k) c9 S9 t+ b3 c
" ^/ C& Y6 I/ f6 [: [5 h: q
8 G0 ^* r$ P/ J" J
5 x# T: }, C/ r0 o/ }0 T% b" \3 t! b- W* h+ a: o
4 i% l7 n% K3 G% g
5 D3 H& L- D" M# I  @
( ^7 T! w' N. d2 r' v6 I
" n8 V  \+ y, z- {' {* U
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero. K+ b! D* Q5 E
Operations to perform:% `8 \9 G& E9 ]& W6 p7 h
  Unapply all migrations: order
( v# k/ a& V/ }7 J  wRunning migrations:
4 E4 l4 @7 ?( F  Rendering model states... DONE
. o& Q0 k* E: b( a) S  Unapplying order.0001_initial... OK& j7 l) n3 J% a1 W

% o/ ?, t' ?5 l2 A( s: h(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero2 y1 W2 j8 h" z1 u- }! C/ t
Operations to perform:* b: b! {# x6 y; U7 q
  Unapply all migrations: order6 X, m) h4 \7 Z/ V+ J, l/ }
Running migrations:
) A) r; S' o" f  No migrations to apply.' {5 s2 S% D; y. f' `4 N
: {, Q8 G# K! J. |5 C
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations --empty order( v4 _# K# p7 O7 K* U& O9 i: x
Migrations for 'order':
! W$ n0 b) {3 h$ m% h6 K  order\migrations\0002_auto_20250718_1752.py
) Z) p8 v# E! B' Q7 p5 T# X1 \4 _) `
! C  t3 _$ A. Q(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell+ ~" c2 H1 O' a2 r3 S
Welcome to the MariaDB monitor.  Commands end with ; or \g.
! D; d5 x0 O8 \* e% |( uYour MariaDB connection id is 162. |& f# I+ B) A- o
Server version: 11.5.1-MariaDB mariadb.org binary distribution
6 Y# M' S) d% r/ f$ x& C4 d, S
, m2 Q# B: d% N# U5 lCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
5 d& I9 T: D9 }( E+ H) X! B
) X/ C  N* P8 t$ `; F7 @Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
/ A: D6 \/ r3 X( k, _$ _1 B$ q+ `9 {# h7 c
MariaDB [tripsql]> delete from django_migrations where app='order';" q6 R$ C* b3 ^
Query OK, 0 rows affected (0.001 sec), t1 f5 f6 m- o( c- ^! {, u
% Q5 V, }6 [+ i: {; m& s7 p0 \* w
MariaDB [tripsql]>3 R$ b; F. b- Q& g! i4 W# d

' }0 X9 y% [& F" P
2 \9 |' p9 X. e* E
; f" l5 n  x* x2 A6 U: B
5 j+ ^7 S  p7 I  W: z" I; \! a6 O& Z2 e! I& O1 ^
* J: C2 n# V/ c2 E

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-7-18 17:22:08 | 显示全部楼层
问题描述:6 y1 |0 ~) ]) v3 ^4 ]& ~+ e% u/ M
已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrations确认成功,执行python manage.py migrate,提示No migrations to apply. 表示一脸懵逼。再次修改,指定表名,再次尝试,发现问题依旧,表示二脸懵逼. e2 F, H# ^5 M1 A% H& V7 h: [" d; @

0 O+ ]7 p( q- Y2 h- Z2 A; q排查过程
, r$ a6 j: P" P' I, H" v! y2 ]python manage.py dbshell 进到数据库里面,查看是否表已存在 # O* d+ y8 P% |0 g# P% b( j
结果:表不存在9 X7 T+ u7 q% Q( |) M1 x3 p
检查migrations文件 $ X2 G5 Y( J, V: A0 h4 ]" T
结果:文件没问题
' R1 g6 c) s0 A% z百度 google 各种搜,乱投医,各种尝试+ \  f, E) U0 V) z
解决方案
3 n: s6 U8 Z# l  y; P4 u6 k" |python manage.py dbshell 进到数据库中,执行delete from django_migrations where app='your_appname';0 ?" |; C1 E" u, E3 R  n$ I/ W' o
python manage.py makemigrations(若migrations文件未删除,可不执行这一步)
- H0 ]& o% m  y; c! K( [- Hpython manage.py migrate 好啦,大功告成  c" c: u. I$ k. @6 f  [8 N
原因分析
, @/ k- A- L9 C查看django_migrations表结构
5 K/ ?9 D0 s% C; J3 O建表语句: " `! L; X$ a/ {3 }* `
CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL);
# Z) U$ C* u* U原因 7 H7 G7 s0 m' M# p  _/ w- j
造成多次应用migrations失败的原因是,当前model是修改过的,原来的migrations已经被我删除,但是,重新生成的migrations使用递增整数记名,所以,在django_migrations表中0001,0002等前面几个数字的文件都已被记录,在Django看来,被记录了就相当于已应用,所以,会出现刚开始的No migrations to apply.# `8 j2 \% x9 P: s1 R- n
避免方案  {2 R8 |: B  D# ^% n
有强迫症删除migrations文件的同学(比如我),请同时到数据库中删除相应记录2 s/ Y8 l6 {' U" D
没有强迫症的同学,可以继续生成新的migrations,旧的就不必理会了
+ v- F* n5 J- }! S, v/ V6 k. o' N" ~9 H题外话
5 ?2 w, n$ V- v$ O& C6 u. F' D2 `1 l8 h执行python manage.py migrate之后,可以使用python manage.py sqlmigrate appname migrations_num(例如python manage.py sqlmigrate user 0002)查看当前migrations文件对应的sql语句。
9 x0 P% d1 r# X另外,在使用上述命令查看0002文件的sql语句时发现,django会新建一个表user_new,然后插入user表中的数据,再把user表删掉,再把user_new重命名为user。所以,修改model的时候,不必担心原有数据会丢失。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:26:16 | 显示全部楼层
MariaDB [loogcn_data]> show tables;
4 F. s( ^/ l5 F7 a+-------------------------------+
6 ^1 _2 u$ n" @| Tables_in_loogcn_data         |
  k" P' Z: u. G+ z) f: v4 x$ [5 Q/ j+-------------------------------+
, p, @( G9 w+ Z0 i! O. h: C6 a| account_user                  |
/ T1 O% x+ i, G* Q8 k| account_user_groups           |+ j( v/ q- p5 x
| account_user_social           |
5 [! r1 P* v" K5 t| account_user_user_permissions |* c  A6 p7 V$ X, l2 S
| auth_group                    |* j6 X* Q* o* _
| auth_group_permissions        |" R, u6 I7 o' c' }
| auth_permission               |1 x& }; s/ D; ?+ W1 \% `
| django_admin_log              |
8 m. ~) x' n( t5 Z8 W) k| django_content_type           |2 o; n% u# x4 i4 \$ a% q% ~
| django_migrations             |  S6 L$ m% B& W. t  J8 ?1 T
| django_session                |, K8 g' |8 L( I$ l+ y1 \
+-------------------------------+
- n  I" Z0 e" K3 q) g11 rows in set (0.001 sec)$ g8 B- m! O9 V" L- i& w5 X0 b% p
" E4 d$ Y/ m& t
MariaDB [loogcn_data]> select * from django_migrations;
$ i" i9 F" b% D- k% k5 \5 S, _+----+--------------+------------------------------------------+----------------------------+5 _7 [$ A$ ~, x8 D* u
| id | app          | name                                     | applied                    |4 c' k) R+ e6 u9 O# j+ ?9 e
+----+--------------+------------------------------------------+----------------------------+
- ]$ h6 c) @, |& v1 p2 O- R|  1 | contenttypes | 0001_initial                             | 2026-05-23 02:00:34.278182 |8 v: `( [! u1 x
|  2 | contenttypes | 0002_remove_content_type_name            | 2026-05-23 02:00:34.505337 |. J0 q5 o, i2 v& A6 _
|  3 | auth         | 0001_initial                             | 2026-05-23 02:00:35.411267 |! ?9 X- A5 Y1 x; Q/ A
|  4 | auth         | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |; r. @) J. U7 w( P6 _
|  5 | auth         | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |6 s4 b0 a  s8 V! k! k/ W; z& @
|  6 | auth         | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |' f& Z' u  S) l* b, t1 p
|  7 | auth         | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |% [! N, s' X2 l* r$ }$ c( p
|  8 | auth         | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |
% r+ b5 ~* m  F* _. |$ y|  9 | auth         | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |
- Y7 t) V. q  X4 }. Z( y| 10 | auth         | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |
8 Z7 u, i8 {& G# [' `| 11 | auth         | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |
) u- ]% C- M7 k, g2 m| 12 | auth         | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |
+ W, b7 _9 `2 x. l8 ?8 n7 || 13 | auth         | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |
2 R1 ]4 o6 B  i6 x% K1 G| 14 | auth         | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |
, P  X  o: i8 X| 15 | accounts     | 0001_initial                             | 2026-05-23 02:00:37.445274 |0 N6 R0 ?2 B  T) G2 p& a, h
| 16 | admin        | 0001_initial                             | 2026-05-23 02:00:37.817254 |/ K5 s/ A$ k! L1 U% b! I' y
| 17 | admin        | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
+ l' A" \# k( P' C4 H" I| 18 | admin        | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |
5 l) m  ^( B: ^9 G+ y! D( d# s| 19 | sessions     | 0001_initial                             | 2026-05-23 02:00:37.991277 |
& {7 i5 P' C4 S9 H+----+--------------+------------------------------------------+----------------------------+2 Q% M2 L6 _8 y: k
19 rows in set (0.001 sec)
+ b- u' k6 s6 w) ^5 S# Z
& \/ f( H8 Y$ g& i& M. TMariaDB [loogcn_data]> delete from django_migrations where app='accounts';
5 h1 j7 c0 u( O, W7 r1 W2 mQuery OK, 1 row affected (0.001 sec)) m; W, w3 u. ]9 h/ z: t

. b& `; C; d& jMariaDB [loogcn_data]> delete from django_migrations where app='contenttypes';
" h3 `0 q/ N% K1 c6 g4 U9 nQuery OK, 2 rows affected (0.000 sec)) a* T" n2 Y& K% @8 [
7 n6 h  D$ o8 Y6 B: g
MariaDB [loogcn_data]> delete from django_migrations where app='contenttypes ';' x" N4 E. j; T9 F& U% j) X
Query OK, 0 rows affected (0.000 sec)
) g* `' u0 {: ?9 `# B
  M* C4 Z6 d& y6 s& Q/ _MariaDB [loogcn_data]> select * from django_migrations;$ s$ X, M; g1 L& g$ @1 _$ B- Y
+----+----------+------------------------------------------+----------------------------+
- _! b- u/ y4 B4 r! U9 E) C! p| id | app      | name                                     | applied                    |
2 j& z$ f( H/ ~. G1 H+----+----------+------------------------------------------+----------------------------+
# D! ~$ O- ^/ r* ~& ||  3 | auth     | 0001_initial                             | 2026-05-23 02:00:35.411267 |0 {- N  `3 q/ a. j8 k& @; W
|  4 | auth     | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |
  G4 l; ~) I2 x% f|  5 | auth     | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |
& @/ T' e4 p. u|  6 | auth     | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |
) L1 t7 L7 I2 d: }|  7 | auth     | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |4 S+ f' W2 ^( \0 B
|  8 | auth     | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |
- a2 ~' L3 Z! Y) m! d: }) o4 k5 H|  9 | auth     | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |
# k$ U7 ?( ^5 e/ e  O, c| 10 | auth     | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |, B6 x1 _4 r0 A& m1 A% W$ `) N) z  `2 t
| 11 | auth     | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |
$ {: X7 {2 b9 d2 D, O| 12 | auth     | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |; F( K" w$ F$ L, [7 k
| 13 | auth     | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |( {( ?' X) D' h4 `$ `0 a$ s
| 14 | auth     | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |
" x+ Q$ O0 U2 y- f8 T) U8 v| 16 | admin    | 0001_initial                             | 2026-05-23 02:00:37.817254 |
; c, Y) ~' u3 w* f) l% \| 17 | admin    | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
- h  n2 Y5 p2 F1 }| 18 | admin    | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |
( f: d+ d% Z. g- M$ S| 19 | sessions | 0001_initial                             | 2026-05-23 02:00:37.991277 |
* [) u, y4 G5 N" W& m# F+----+----------+------------------------------------------+----------------------------+% c  Q1 Z& ~4 q: H
16 rows in set (0.000 sec)1 X3 V/ Y0 v" m: D, K& k! T& @
5 H# `4 y+ U# N3 T& B
MariaDB [loogcn_data]> delete from django_migrations where app=' sessions ';
) s% R1 z# J; p* A: Q; `Query OK, 0 rows affected (0.001 sec)- Q  y' O8 n7 ]3 K$ M; _

! w  }5 u& ~" E; F5 f8 t2 UMariaDB [loogcn_data]> delete from django_migrations where app='auth';
) ?7 r' |% g  ^% p$ K5 YQuery OK, 12 rows affected (0.001 sec)
/ q; b$ P; O: O& D, P8 Z! x+ L: g- q% p; d
MariaDB [loogcn_data]> delete from django_migrations where app='auth' and app='admin';% l  |9 [. H0 t8 L; }5 L, d
Query OK, 0 rows affected (0.001 sec)$ A; P, Z# s+ L; |
$ s5 s( m7 e. y% I$ t
MariaDB [loogcn_data]> select * from django_migrations;
6 h% ]9 c8 `- e6 |+----+----------+---------------------------------------+----------------------------+6 _/ Z6 L; D3 p% ^4 G3 G. \  \
| id | app      | name                                  | applied                    |/ Y4 H+ A! f6 M+ @1 |
+----+----------+---------------------------------------+----------------------------+
7 M( c$ x1 `# K# @% ^| 16 | admin    | 0001_initial                          | 2026-05-23 02:00:37.817254 |+ X3 E1 x9 O$ E9 e# Z
| 17 | admin    | 0002_logentry_remove_auto_add         | 2026-05-23 02:00:37.827664 |
& z6 {2 A; d7 j  g* w' O| 18 | admin    | 0003_logentry_add_action_flag_choices | 2026-05-23 02:00:37.836978 |
1 W7 G% u. {- U4 O| 19 | sessions | 0001_initial                          | 2026-05-23 02:00:37.991277 |+ ~0 z, T+ H1 N8 C" O
+----+----------+---------------------------------------+----------------------------+3 `# B4 B1 v4 d% G+ W4 K$ I
4 rows in set (0.001 sec), S) i( @+ l  P
4 |3 l2 N8 q0 J0 N4 f
MariaDB [loogcn_data]> delete from django_migrations where  app='admin';
  r, ~4 A. l, u, ^6 bQuery OK, 3 rows affected (0.000 sec)
. {; \- r- Y9 O  U7 ]
8 d. m  k0 t9 ]% G# m2 i# }2 Y0 W9 i# cMariaDB [loogcn_data]> select * from django_migrations;
  C0 Z' U8 o. f+----+----------+--------------+----------------------------+
7 c" `' [& c2 s4 F, U| id | app      | name         | applied                    |
0 w, Z. @# ^. }5 t" \+----+----------+--------------+----------------------------+
, N% @/ ?* P- l) o' A| 19 | sessions | 0001_initial | 2026-05-23 02:00:37.991277 |# J. r( f# y& b: d
+----+----------+--------------+----------------------------+9 B+ s) E0 ^6 R) Q
1 row in set (0.000 sec)
% L; |5 h1 l" [& M, P0 c3 G
6 a/ w- A& m3 {& K3 |( m, v! tMariaDB [loogcn_data]> delete from django_migrations where  app='sessions';2 p0 u: A: ~! l- ?7 w0 \# Y# z4 G
Query OK, 1 row affected (0.001 sec)
  T0 R- h/ M9 |+ I1 I7 [) s  @8 @; s( r
MariaDB [loogcn_data]>
. v. z. d: d; w/ _6 J 清空所有的表,
2 L1 O) x6 S) w8 d8 l  C

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:27:29 | 显示全部楼层
进入 MySQL 终端,删掉当前数据库并重建:sqlDROP DATABASE your_database_name;
) t) s+ M- ]2 v' ^  ICREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
4 x2 a+ D1 e( v5 T6 z5 g5 a请谨慎使用此类代码。重新在终端执行迁移命令:bashpython manage.py migrate/ k. A. I% W5 ^% |( h
请谨慎使用此类代码。如果你使用了方案一或方案二后遇到了其他报错,请告诉我:这是新创建的项目还是维护中的老项目?数据库里是否有需要保留的业务数据?我会为你提供更精准的修复步骤。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:33:03 | 显示全部楼层
MariaDB [loogcn_data]> show tables;- m  h3 _8 x7 \2 I
+------------------------+
* B0 Z3 i. X  Z0 {* ~7 Q8 || Tables_in_loogcn_data  |
/ V3 K9 A; W+ K4 [3 E1 m4 S8 j+------------------------+0 [3 }, L% v/ ]) T; l) g9 t
| account_user           |9 e9 ]! O( s- e
| auth_group             |
1 X+ L3 ?0 V0 T2 B( W| auth_group_permissions |4 e& B. C" V, s
| auth_permission        |
% b% i0 ~! T% M0 C' P1 a* K| django_admin_log       |
1 w/ X0 X5 R! ^1 |: U| django_content_type    |
# ~; |! W/ N+ ?" O; ~, w| django_migrations      |
$ a( q5 r' h; \# a: s) `| django_session         |
6 k( g7 W7 P& a, ^0 o+------------------------+  H) q$ C: u' o! T7 g! V1 H, c- F
8 rows in set (0.001 sec)
5 S1 Q- {- \9 U+ O* R9 I. h) I8 @0 ]; I  _7 K/ J0 S" [
MariaDB [loogcn_data]> drop table if exists auth_group_permissions;1 ~/ Z% M3 D& ~; _9 |
Query OK, 0 rows affected (0.058 sec)" _3 H# }' j, x6 @; L

) [+ S/ b5 g; bMariaDB [loogcn_data]> drop table if exists django_admin_log;
) _( e% s4 P& sQuery OK, 0 rows affected (0.056 sec)
! d; ~* G0 M& f7 P7 Q1 I1 k" B# q
. u% b' F3 Q! |/ D- OMariaDB [loogcn_data]> drop table if exists auth_permission ;
3 `& a/ s  }+ W1 SQuery OK, 0 rows affected (0.061 sec)7 d7 s% \6 M+ o: _% C, [0 e7 H

2 r3 I0 O3 ]- B; [" mMariaDB [loogcn_data]> drop table if exists account_user ;
! x) V' e/ e8 D7 T8 K. G7 ]: `Query OK, 0 rows affected (0.060 sec)" u9 o' W3 I  v% k6 ~

' b9 u2 _9 Q% }/ u& @MariaDB [loogcn_data]> drop table if exists  auth_group ;
+ p+ W9 v" h+ N, @Query OK, 0 rows affected (0.055 sec)
( V6 ]1 |7 q" O
: x6 a: ^2 h: x6 [0 M/ ~! o/ ]3 iMariaDB [loogcn_data]> drop table if exists  django_session;
/ |' M  _  M1 X/ U( RQuery OK, 0 rows affected (0.051 sec)! p7 t) w  g$ [# W

/ w" p! f0 [/ l0 `" SMariaDB [loogcn_data]> drop table if exists django_content_type;
# Z1 D, n1 V; n9 o( I5 EQuery OK, 0 rows affected (0.052 sec)
* [4 B* L' `6 {
2 O: O, i4 E  f. U' N' RMariaDB [loogcn_data]> drop table if exists django_migrations ;" k5 S$ _2 U1 |6 x  {- W; ]' v
Query OK, 0 rows affected (0.068 sec)+ R+ E% ?. V7 P9 O! e% s4 T8 F# U: `
* p* {2 p3 ~9 E! n
MariaDB [loogcn_data]> show tables;
5 }; v0 C5 O% {: s0 yEmpty set (0.001 sec)
& s: J- C% Y0 l" a# R. N* R, A3 ~& G$ d5 N
MariaDB [loogcn_data]>9 y* |) a# K) _6 x. L9 O5 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 00:10 , Processed in 0.016773 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表