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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-7-18 17:17:02 | 显示全部楼层 |阅读模式
因调试过程中,发现有点问题,就把数据库表从其他客户端删除了,以为删除了就可以同步过去,没想到就是下面的结果。. Z' ]1 q: {4 q+ n( ]: H2 a, b
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py check0 v2 }) o: b* h, b! ^
System check identified no issues (0 silenced)." R' F$ c" n: X1 U2 r. F8 w) k& C

5 X% j8 I% b. c- S2 p' J(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations7 y$ L/ a  i3 t  o
No changes detected2 c3 Z  j: \1 E

. f! D, O8 m) H$ `(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations order
8 Z- [! _) j  |No changes detected in app 'order'
6 d9 p3 ~- b% ?+ d# U$ Z
( T; p# b- f: p2 U: J(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order9 Y+ E) P" z* _4 n$ B9 x3 h
Operations to perform:$ p* L+ k* ?5 C% L9 m; ~+ i+ s: @8 U
  Apply all migrations: order% ]& B4 z, M- \- b* ~/ o+ p& c
Running migrations:5 w" k5 n/ U6 U2 X! A' J
  No migrations to apply.3 R7 Q! D  y3 y

: o( |' N" Y9 R9 u: v同步不过去,怎么办?
  a. C1 z, I# J4 h# }0 {* Q看到有高手的同步经历,按照他的方法,去执行了下,好像问题解决了。
* V) O) _, e' d# {  F- S8 R& w; s& T. G
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell6 t9 j0 k) G  i* K  e
Welcome to the MariaDB monitor.  Commands end with ; or \g.5 {6 U5 A0 t% S% H
Your MariaDB connection id is 152
4 z4 p+ X5 L. H6 wServer version: 11.5.1-MariaDB mariadb.org binary distribution& f( c* w' h& l  W5 L
. [: T9 u- p+ _: t
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  T+ C+ E6 S! n9 q
: }' ]) S% L1 a/ z4 qType 'help;' or '\h' for help. Type '\c' to clear the current input statement.
$ |3 F$ J. K5 O# p! P3 q8 ~
  S" y6 F' [" E$ L# KMariaDB [tripsql]> show tables;
: ~4 U$ f: ~1 P) P0 J% E. Z) @9 v$ }+-------------------------------+9 f7 s  o& x: P
| Tables_in_tripsql             |
: ?; _' e% Z! v2 P+ u! O/ |; D) l+-------------------------------+
: k( G$ o( U* f| account_user                  |0 \4 ]% z6 g8 D! G+ V
| account_user_groups           |" d# d! q0 @- @% i, c3 a7 B
| account_user_user_permissions |
* Y. L2 G; I! M+ a| accounts_login_record         |
. |/ m* T3 Y! U- O: S6 p| accounts_user_profile         |* T& T* R$ `6 C4 ]
| auth_group                    |" K, A1 V( A3 o- W% l0 W
| auth_group_permissions        |. k8 N. }/ ~( K2 X
| auth_permission               |# D* ^$ t+ h( o) [4 U6 ~
| auth_user                     |! N4 ~, y: }2 X2 Z. J
| auth_user_groups              |
+ T, }% D/ p4 M2 S" T& f! {| auth_user_user_permissions    |2 J+ W, H& l; A! M% O% l
| django_admin_log              |
2 f7 ]2 v) v4 }* i2 v, B| django_content_type           |- H7 ]7 h7 W$ J: Q/ `  L
| django_migrations             |5 J+ t* u: m& S& w* {. ~9 f
| django_session                |  k# d! N+ i6 D- J/ o, i
| sight                         |; F4 n2 D5 N# e4 B, L7 w" R6 l  ]- g
| sight_comment                 |) ?0 _7 \& j  I, o; ^% j4 b3 k
| sight_info                    |; D1 i2 Q9 G6 R) L8 F
| sight_ticket                  |
& T+ T% J7 t8 I1 H8 l9 E" \+ O8 g| system_image_related          |
2 W0 k: p2 Y7 p$ }8 A| system_slider                 |; M1 R0 E8 g$ C7 u
+-------------------------------+
# }# D/ R6 V& S7 D) k, R; @) R1 g21 rows in set (0.002 sec)4 }  W% Y: e. e) D3 Y

+ U1 T2 W# Z: v& `% I. Y
. {1 Q5 Y" O' Z, o2 g9 K% _MariaDB [tripsql]> delete from django_migrations where app='order';
4 r9 Q; H- u) I; E7 ?7 }+ ?' oQuery OK, 2 rows affected (0.004 sec)# [( f6 [2 \& r; M: g" C! Z

9 \' o4 h8 t; y  k6 w; XMariaDB [tripsql]> exit' Y4 Z# y% _+ c) W  ~' S
Bye% S4 l( i! n& a& Z" I& X: _" G- W
+ E& T5 x( H  o: K, Z/ m
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order2 C+ w3 O9 h9 z+ W: I! L. w/ Y
Operations to perform:
: j  |# l" z) D' F; `; o& _. O7 A  Apply all migrations: order
$ @4 L, D* \6 B3 ^Running migrations:
% [& m; z% k9 q0 q  y: Q# `4 ?" X  Applying order.0001_initial... OK
# D. p: j' N- Y* N' c: L0 p
. F& o5 i) p/ J9 j. Q$ v2 U6 U3 J. l(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>
: t2 E8 f% D' t6 g4 ^3 w6 C0 r" l这次成功同步过去了。9 r0 d8 x/ a& J8 T( l

0 }$ S& F5 ~6 d5 n  c. w$ x1 Y3 v3 ^- X. t& F0 k: J; c

7 s2 o8 Y0 m: }1 V' S3 l. }( E6 l2 S- }+ d
* E0 D$ [! u* W

  R; y* c( @0 l+ ~3 f- j9 N9 B6 V! h2 S8 x! c4 n1 O
5 l7 b3 \. N9 A/ ]3 |
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero" v# M& ]0 ], G+ r
Operations to perform:
4 W8 h$ |' _  R# W  Unapply all migrations: order
; {% S) w7 I' z8 f2 {9 mRunning migrations:
; A+ o; _% F& q: ^( u1 U  Rendering model states... DONE
, z# |- s) i' J0 }. Y  Unapplying order.0001_initial... OK' R/ r3 V3 N" z1 ~1 p

  K! j2 g" J( y/ g8 L4 Q( O1 t& d/ r9 t(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero+ R- [. i% C% G+ `# K- c6 x% Q
Operations to perform:9 Q8 \2 J( }: _4 ]/ D" ^$ N2 E
  Unapply all migrations: order: a" X) d1 X  b5 ~* x
Running migrations:2 ^) @( `0 @, V( ]3 X1 f" m9 ]
  No migrations to apply.
5 R; ?3 n' }  O( Z, S# N' _3 [; o, S5 b
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations --empty order- ~7 ]# F4 a/ Z6 }
Migrations for 'order':
! _1 p0 e# S  _! h  order\migrations\0002_auto_20250718_1752.py
" u7 A% g' Z" L; N
5 R7 ^* t9 ]- J- [1 p' N(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell
, ]- K* a! k+ j5 G0 `Welcome to the MariaDB monitor.  Commands end with ; or \g.
% Y5 v' `! Q; N0 _Your MariaDB connection id is 162
4 I7 Z- [' F, a$ K# X! [  {: v0 WServer version: 11.5.1-MariaDB mariadb.org binary distribution
; y3 B9 b$ W) d) s) j) E* ?/ X7 ?
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
' `- H5 a1 g$ w6 S3 ?7 v. w2 E
! Q; _# k& |9 CType 'help;' or '\h' for help. Type '\c' to clear the current input statement.+ c2 P  H( C9 B

2 C% ?1 }. q; E; zMariaDB [tripsql]> delete from django_migrations where app='order';- w  h6 b1 c( O
Query OK, 0 rows affected (0.001 sec); |9 N2 G0 G# r1 J4 N0 x
! c3 m% B( G: _& O6 u
MariaDB [tripsql]>
1 A" _/ S- l! Z& Y' z  ]4 d. X) p6 n

/ q9 N; k4 L9 I, o8 {. E. L& m
7 ?. g. l5 e$ b  X( J7 Q! j. H9 U0 a5 P0 B5 [* r- o0 D
. E6 _& p- Z* K7 p/ h& v& o
: V2 {" ^9 j( h' j( m, |4 n

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-7-18 17:22:08 | 显示全部楼层
问题描述:* Q% Q+ A0 a9 j* V$ X" u3 k! H
已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrations确认成功,执行python manage.py migrate,提示No migrations to apply. 表示一脸懵逼。再次修改,指定表名,再次尝试,发现问题依旧,表示二脸懵逼" V" F0 n+ n: h7 y/ U

% h* Z" U$ t! p3 u; n* B5 t5 S排查过程
$ E2 ^$ k1 |- n1 X& H5 p. \. g1 @4 Bpython manage.py dbshell 进到数据库里面,查看是否表已存在
  D$ T) }/ e$ T* V3 z6 E, Y8 V+ @6 q结果:表不存在
( }% _! W8 h- B6 j检查migrations文件
8 _0 n" d! o& w( f结果:文件没问题
3 E! C7 W! Z5 f; b* o# z& d4 }% N& K% s$ M百度 google 各种搜,乱投医,各种尝试% A3 H- F7 H% w5 `" Z
解决方案
6 R( n) s( I* C3 Y7 j# h; z4 l6 Dpython manage.py dbshell 进到数据库中,执行delete from django_migrations where app='your_appname';, E4 C# E) b" x2 {
python manage.py makemigrations(若migrations文件未删除,可不执行这一步)$ T' [3 D9 h& ~
python manage.py migrate 好啦,大功告成
: n9 G8 `5 a! I原因分析/ G. A* G, |7 `2 @2 X5 c
查看django_migrations表结构 $ b2 R$ Q# |8 e! t
建表语句:
- d. C0 }* S! a7 n7 U- J  `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);
' u, q4 D5 E2 P# ]4 ]原因
: r! W: ^* U; @; m" w* m造成多次应用migrations失败的原因是,当前model是修改过的,原来的migrations已经被我删除,但是,重新生成的migrations使用递增整数记名,所以,在django_migrations表中0001,0002等前面几个数字的文件都已被记录,在Django看来,被记录了就相当于已应用,所以,会出现刚开始的No migrations to apply.
0 e8 m9 g5 z9 Y! c避免方案1 S! k6 Q$ l) C1 N
有强迫症删除migrations文件的同学(比如我),请同时到数据库中删除相应记录
- y0 e4 r( W9 n6 f: Q3 y7 Z没有强迫症的同学,可以继续生成新的migrations,旧的就不必理会了
5 g6 \1 ^* L  B2 p+ X6 n题外话5 C% k" B! v9 P1 r, B
执行python manage.py migrate之后,可以使用python manage.py sqlmigrate appname migrations_num(例如python manage.py sqlmigrate user 0002)查看当前migrations文件对应的sql语句。
. h9 W2 _5 L+ m7 m! F, B另外,在使用上述命令查看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;
/ I( q( r; D) b4 `$ P% h" e+-------------------------------+1 d) ?# J' }: @
| Tables_in_loogcn_data         |+ Y) Y2 Q% q8 B% V2 w% q- `' I
+-------------------------------+
7 Q, @( o! N/ O% Q* L2 K% d% f| account_user                  |' h1 ~, i& t, v) g
| account_user_groups           |
/ d% R9 x2 U$ P' w7 `' D$ l| account_user_social           |' g& i, A  O! C
| account_user_user_permissions |! I" m) L% s" H4 l- y0 U- s
| auth_group                    |
# O9 y. ]" S% j9 o1 q| auth_group_permissions        |
% o( {  |( X4 f, I* [| auth_permission               |
6 G+ o& Z" _2 T0 h8 R' M| django_admin_log              |
  S0 w/ E- `7 a| django_content_type           |6 P0 _8 l) I% W9 G
| django_migrations             |
1 m" X( u" y7 x8 s# E- N| django_session                |6 U5 T4 c0 e5 Q4 {# @) |" w- v
+-------------------------------+8 N2 D* h0 s# w2 c* d) u7 q9 p
11 rows in set (0.001 sec)+ i1 P& ~. ^. [. ~; p6 \0 c/ w6 v
8 H. G* s  j" P
MariaDB [loogcn_data]> select * from django_migrations;+ ?( n+ {" p- H' z: N1 F
+----+--------------+------------------------------------------+----------------------------+
$ m" S1 \8 ^# A$ H! H- @0 N: [| id | app          | name                                     | applied                    |& H( i! Y& T9 ^" c# A0 ^" Y6 `
+----+--------------+------------------------------------------+----------------------------+9 l5 l( b$ o& B5 \; m3 ~) v
|  1 | contenttypes | 0001_initial                             | 2026-05-23 02:00:34.278182 |
+ B: e! b2 v: q5 T0 @|  2 | contenttypes | 0002_remove_content_type_name            | 2026-05-23 02:00:34.505337 |
& h) ^: e9 [; R3 g3 v: w7 _  a4 h|  3 | auth         | 0001_initial                             | 2026-05-23 02:00:35.411267 |  _8 G6 R& ^* W
|  4 | auth         | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |
7 u, ^$ R' R; ^" V" ?! w2 K|  5 | auth         | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |
: y3 l; i$ ^$ D/ O/ h7 b5 M  B|  6 | auth         | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |/ T4 E* o) k& k) g8 y7 r
|  7 | auth         | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |5 J* M+ x  B% A9 ^
|  8 | auth         | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |0 E# z- D+ g* E& j! i* ~# P
|  9 | auth         | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |
% h, ^/ F0 c& a  a# U' S5 G| 10 | auth         | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |: L+ n1 I& H, e9 B" e1 Z
| 11 | auth         | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |/ r8 i' y+ J9 z, ^
| 12 | auth         | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |( E1 `0 `' d: F3 n2 Q# M) p# L
| 13 | auth         | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |) g% |2 I% l  H
| 14 | auth         | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |
, J1 c+ J7 N* d% u| 15 | accounts     | 0001_initial                             | 2026-05-23 02:00:37.445274 |- g' R+ K9 y# q/ \& y
| 16 | admin        | 0001_initial                             | 2026-05-23 02:00:37.817254 |: j) U0 D/ P5 E0 W# ], |6 z4 r" h
| 17 | admin        | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
6 k7 c, d1 t3 [2 [| 18 | admin        | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |7 a# C' V) R7 Z. b( w+ Q. J5 [
| 19 | sessions     | 0001_initial                             | 2026-05-23 02:00:37.991277 |
$ C8 J4 |5 s, s. Y+----+--------------+------------------------------------------+----------------------------+
8 W2 p1 c2 L8 L9 V* [/ Z% d19 rows in set (0.001 sec)
+ j2 t9 v) F" ^" M( G( K
$ A" K+ [$ V6 @6 h: t) J2 w; Y4 e2 Q8 kMariaDB [loogcn_data]> delete from django_migrations where app='accounts';$ m7 z+ T! x4 `0 ?6 y
Query OK, 1 row affected (0.001 sec)" p4 v) `+ g/ {9 Z# `6 f' q( W

  ?4 W, ]: Y7 r+ s& {MariaDB [loogcn_data]> delete from django_migrations where app='contenttypes';4 V2 x$ D* s/ \. d+ V6 g5 y0 X/ P; p
Query OK, 2 rows affected (0.000 sec)3 S0 [* }$ I( a% J

8 E1 [* w4 h/ ]MariaDB [loogcn_data]> delete from django_migrations where app='contenttypes ';
1 ]4 P8 x$ u8 L% l/ m0 w# FQuery OK, 0 rows affected (0.000 sec)
8 k2 |$ t, v" R0 y# Q2 f0 \! F! s
3 P( Z" ^5 n& c2 @; {MariaDB [loogcn_data]> select * from django_migrations;
% U9 f; @( y0 q' g  V; p  T+----+----------+------------------------------------------+----------------------------+" f- v/ b. P7 n5 z
| id | app      | name                                     | applied                    |
  s+ A' X" ^- \. I" f' R; J" u+----+----------+------------------------------------------+----------------------------+
0 Y- o/ F2 a$ P|  3 | auth     | 0001_initial                             | 2026-05-23 02:00:35.411267 |
: g( \  o2 d/ H/ X|  4 | auth     | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |. H' [  f/ j2 _! _, B0 m
|  5 | auth     | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |
; v9 S9 F/ i" T  n2 X/ ^8 I9 x2 U|  6 | auth     | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |+ r% _. o7 ]. [5 l! L  s! [4 a
|  7 | auth     | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |# w5 Z1 Q" u7 h  C
|  8 | auth     | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |
/ ], x4 b) b+ k" T- W% D+ I" T/ A- a* O|  9 | auth     | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |) d! m- f1 H  q  q) T& j
| 10 | auth     | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |5 Q4 W3 j0 q% v
| 11 | auth     | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |' `& N! j9 ^/ R/ N
| 12 | auth     | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |3 @1 S/ _. A' d! [) g0 k6 H2 Y4 y
| 13 | auth     | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |
* L; ?! p' r, {+ {9 k  M& _| 14 | auth     | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |8 O3 ^( c8 S4 L5 d
| 16 | admin    | 0001_initial                             | 2026-05-23 02:00:37.817254 |
# L1 }! u% @5 f2 }$ b| 17 | admin    | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
3 v7 y2 H/ g4 }% G* e| 18 | admin    | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |
0 u& ?7 m% |, G| 19 | sessions | 0001_initial                             | 2026-05-23 02:00:37.991277 |
& y* I* y) Y, X: C6 c+----+----------+------------------------------------------+----------------------------+$ r: q' B5 D+ p: t
16 rows in set (0.000 sec)9 A3 t9 E9 v  T' X7 z
5 k' a* a% W) s1 w1 [8 O8 l9 ?# J
MariaDB [loogcn_data]> delete from django_migrations where app=' sessions ';$ V# S& \  [/ C4 d) a: ^& u
Query OK, 0 rows affected (0.001 sec). a& m! [  Q$ h7 [
8 h% E8 X; {$ E) i0 \2 k
MariaDB [loogcn_data]> delete from django_migrations where app='auth';* c0 [' O. n+ \% {: U
Query OK, 12 rows affected (0.001 sec)3 Z. X( |1 l3 {+ r3 Q, Q% P

; M9 F' V/ S7 f# |- k7 ~6 nMariaDB [loogcn_data]> delete from django_migrations where app='auth' and app='admin';- f% J8 d6 B% U5 @2 T3 O
Query OK, 0 rows affected (0.001 sec)- Z6 s/ K7 R. g! t
1 s6 ~' L; N8 @' S9 }1 r5 \/ X
MariaDB [loogcn_data]> select * from django_migrations;! ^' b3 [' [% I" l1 N% a" c
+----+----------+---------------------------------------+----------------------------+
( @4 m6 B) U- Y( O; r% C  }| id | app      | name                                  | applied                    |
: v! [* e. Q2 H( d- w+----+----------+---------------------------------------+----------------------------+
- \$ r) D$ X; \| 16 | admin    | 0001_initial                          | 2026-05-23 02:00:37.817254 |
- v7 q: _# j, Q6 w8 @5 w3 ?| 17 | admin    | 0002_logentry_remove_auto_add         | 2026-05-23 02:00:37.827664 |" r* |6 ]' B5 F% F0 m& ~. t- Q8 k* w
| 18 | admin    | 0003_logentry_add_action_flag_choices | 2026-05-23 02:00:37.836978 |, p9 S* j. d$ _/ G" z+ a7 H( g
| 19 | sessions | 0001_initial                          | 2026-05-23 02:00:37.991277 |% B4 T2 M4 `. Z  I! X. D6 U
+----+----------+---------------------------------------+----------------------------+2 r# ]: ~# p$ ?. f% p/ c3 _) H
4 rows in set (0.001 sec), g: p8 m3 W# A- R
7 Y" E; X5 s1 W: Z+ P
MariaDB [loogcn_data]> delete from django_migrations where  app='admin';9 T- x3 F. y) I8 z$ r" e
Query OK, 3 rows affected (0.000 sec)7 b; p/ ]$ V3 H: M2 J

: K2 N, E  x* bMariaDB [loogcn_data]> select * from django_migrations;
; q2 b: p( M: t+----+----------+--------------+----------------------------+3 M0 U& b7 o# z# ]  H
| id | app      | name         | applied                    |
* l; g! G' Z' ]) X7 B% ^% s0 p+----+----------+--------------+----------------------------+3 B  z8 J9 I) S' a7 z  O% {; P
| 19 | sessions | 0001_initial | 2026-05-23 02:00:37.991277 |
# g, `. G" P1 h1 G- r4 |+----+----------+--------------+----------------------------+
' X9 ~7 f% _0 M% j/ \1 row in set (0.000 sec)
0 f0 Y( T3 k: f, F7 M6 G8 A+ R; _. I/ s' I( [& O2 l# e
MariaDB [loogcn_data]> delete from django_migrations where  app='sessions';$ H5 ]0 H5 p5 T3 L4 o! u. I
Query OK, 1 row affected (0.001 sec)4 @8 H3 x& g9 v1 p- P  E

" a& k% {9 v  F2 TMariaDB [loogcn_data]>
$ g! S" h) s5 n5 Z 清空所有的表,
- {+ t/ h# w$ y

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:27:29 | 显示全部楼层
进入 MySQL 终端,删掉当前数据库并重建:sqlDROP DATABASE your_database_name;
  r5 v$ y4 v3 x1 L: _4 n. fCREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
& c+ e6 p: o, @$ {) C: S请谨慎使用此类代码。重新在终端执行迁移命令:bashpython manage.py migrate/ F& _. P$ Z! S" v: R0 u3 d0 g' Q7 @8 A; T
请谨慎使用此类代码。如果你使用了方案一或方案二后遇到了其他报错,请告诉我:这是新创建的项目还是维护中的老项目?数据库里是否有需要保留的业务数据?我会为你提供更精准的修复步骤。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:33:03 | 显示全部楼层
MariaDB [loogcn_data]> show tables;+ A  R' R" \$ ?4 H: b& O# y: M
+------------------------+. H4 W3 Y1 n- A3 g3 c0 h6 N
| Tables_in_loogcn_data  |! o) V; L- y6 F9 Y0 r% R, E
+------------------------+
# @1 U/ q; I/ t) || account_user           |
) b% n  U3 f+ \7 c) U7 P! }| auth_group             |8 l# P/ R/ D, a$ f) |/ K! @2 C
| auth_group_permissions |  W9 s5 ?' J0 g$ u
| auth_permission        |' ~$ V! g7 F' L8 M- W
| django_admin_log       |# P+ Q/ N$ }- ]  R8 t' k
| django_content_type    |
* T6 s% h& g2 [8 m( E| django_migrations      |7 o# E1 S6 A8 B. W
| django_session         |
! Q( h8 z$ W! A+------------------------+
6 n" G( B. R2 u8 rows in set (0.001 sec)
" n, O/ R- D1 m1 m- c
8 R% D, W/ e3 v. Q% g* r5 ^7 vMariaDB [loogcn_data]> drop table if exists auth_group_permissions;! ?2 ^& j+ h7 w  K$ h. ^4 w: @
Query OK, 0 rows affected (0.058 sec)
; R& R  g) N$ I* Z! L8 ]0 ]
; R0 o, ^8 d0 ~8 b9 _/ ?8 }) A* GMariaDB [loogcn_data]> drop table if exists django_admin_log;
+ T$ I6 G4 X' ~4 D1 [# D# ~Query OK, 0 rows affected (0.056 sec)4 j9 Q: u; x  |& @& @0 |3 m. @

/ ~7 J3 J& i/ @" U. y0 dMariaDB [loogcn_data]> drop table if exists auth_permission ;4 `, r& ^" V6 `9 k" F6 Q
Query OK, 0 rows affected (0.061 sec)
9 M6 S: |+ Z7 O, L
4 s" H' t! M, S$ oMariaDB [loogcn_data]> drop table if exists account_user ;& H/ `- [/ y5 G! s9 T; j9 y
Query OK, 0 rows affected (0.060 sec)& _, j0 A: Q0 p9 ]3 A/ o

* `( R. A: t9 J" w, iMariaDB [loogcn_data]> drop table if exists  auth_group ;
' I( C: r: z+ Y. Y. x  G9 f, |Query OK, 0 rows affected (0.055 sec)
# \  @: p+ D3 v& O. k% Q# i( F; _- d5 ]( c! @$ J3 c
MariaDB [loogcn_data]> drop table if exists  django_session;2 n. r: C) ^5 c1 o. Y8 f
Query OK, 0 rows affected (0.051 sec)
% l1 ]6 W# f' m# L  w
& Q6 D6 t+ Y% aMariaDB [loogcn_data]> drop table if exists django_content_type;0 e' M2 S( x. l9 z
Query OK, 0 rows affected (0.052 sec)
! I* y" j) l- K. a+ _( r5 T& q$ p4 M$ H* M5 ?, e1 l
MariaDB [loogcn_data]> drop table if exists django_migrations ;( R& Z) w/ Z  t- B* M+ [8 M( c' w/ B
Query OK, 0 rows affected (0.068 sec)/ t( M# s  H8 u
3 G+ L+ g9 z: [
MariaDB [loogcn_data]> show tables;
9 I! u2 l  @7 X) M/ gEmpty set (0.001 sec)
, q& l  H7 c, x" j  M2 k$ h  r; q- M. d& M6 w1 A6 l1 V8 ~2 K3 i
MariaDB [loogcn_data]>
! C5 `5 z1 [) c7 _( y5 C: L* R
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-11 23:20 , Processed in 0.014615 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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