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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-7-18 17:17:02 | 显示全部楼层 |阅读模式
因调试过程中,发现有点问题,就把数据库表从其他客户端删除了,以为删除了就可以同步过去,没想到就是下面的结果。
# i# d& a6 q  Z0 s7 u(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py check9 u, ~+ E) P$ T, L
System check identified no issues (0 silenced).0 F1 F* `, E% T$ G& q. V! m

" X2 B2 k3 v3 R6 y(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations
! W. J- f( f; K% Q6 O/ {No changes detected
1 M' v8 z2 \; R8 b; L1 B' {7 t( A" S% O
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations order4 B: \9 H4 E7 r* i% O5 o
No changes detected in app 'order'
4 K3 ~: J4 [& a; E9 H; g  \& q) ^/ y+ T8 @
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order4 Y! K9 g. O" v8 ]! C0 q
Operations to perform:, J* X- J5 N9 l2 ?8 M7 c: t
  Apply all migrations: order
* K% M5 Z  L& S# sRunning migrations:
* t4 y8 `: a. F  x5 a  No migrations to apply.  d% `& R; E3 G: r% \
6 U, T) B' X4 D5 r# k. b+ r% f
同步不过去,怎么办?7 @- D& Q7 H$ U3 ?# r5 S! F
看到有高手的同步经历,按照他的方法,去执行了下,好像问题解决了。
/ H6 A& g) v  W" k( D* [" N8 S6 b& T& |" e$ c+ a3 l! j; W
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell
. w% C9 i! }# k! GWelcome to the MariaDB monitor.  Commands end with ; or \g.7 u9 d5 H" X5 \% q: a
Your MariaDB connection id is 152# K) J) S  A; g- O! r7 I
Server version: 11.5.1-MariaDB mariadb.org binary distribution1 a8 w1 Z9 X1 Y- ?1 P- T

- X# A6 T9 D2 W0 |/ r! RCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
# X+ y- \5 A. m4 I  B% J" O3 Q5 Y: @
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
9 `2 w2 W  b. B& ]* N: f+ H2 F3 f$ h% L5 T
MariaDB [tripsql]> show tables;$ S3 x' \8 r! V$ a! N# T# r( r4 ~
+-------------------------------+
3 ?8 _5 U, @" S  }  ~) U| Tables_in_tripsql             |( p7 g; S( ]+ q" [) K" i3 ?
+-------------------------------+
' [6 I' n' W8 W5 T8 K| account_user                  |
; U5 P2 H7 y9 _) }| account_user_groups           |
! {2 B6 _, C( I' V1 u% s6 |# ~| account_user_user_permissions |8 C# L: d* p4 E- b: {& G: j
| accounts_login_record         |( P9 e4 c8 x+ N) F' \
| accounts_user_profile         |& v; c& O: e+ ~" m6 b0 m- M# M
| auth_group                    |* t& M5 K* j6 O8 Y. `; n" X/ E
| auth_group_permissions        |! ?) ~  A4 ?2 g( z6 Y* x
| auth_permission               |
# S* u  h8 k& c4 _2 t2 R6 Z| auth_user                     |8 z# l6 a# ?+ H( i6 S
| auth_user_groups              |
9 m) U8 ?# {" h/ @& F3 G| auth_user_user_permissions    |
8 X$ R; ]9 z, B) K+ n( k' l| django_admin_log              |
8 ?# S# V- g/ S+ C8 ?7 B| django_content_type           |
  k8 f& i8 T, l9 e* i| django_migrations             |
0 h8 c: s7 e0 E) G# ]| django_session                |9 p; ]9 T5 y9 h' d3 b, _" n
| sight                         |3 N; \: I" D% h9 C& Q
| sight_comment                 |
0 X- N* F3 b) z| sight_info                    |; d$ c# l  E- H5 L3 p
| sight_ticket                  |9 e( @% K' S* i6 T8 p: n7 m
| system_image_related          |
. x. t/ k  Z$ @; n# l1 B$ v$ \: g| system_slider                 |
. D; Z+ Y" d* C# x+-------------------------------+6 _4 V0 Z6 m% k
21 rows in set (0.002 sec), L7 r+ K: |3 Q( I2 B2 J! n

5 Y+ e. u4 }# O, F" X( `0 s1 S& ?$ V, v& j: D! _( h! @" B
MariaDB [tripsql]> delete from django_migrations where app='order';
4 E1 _' `3 P3 v2 ]2 X* \+ TQuery OK, 2 rows affected (0.004 sec)
5 W( E( r0 `7 K' G
- |1 }! _& l& h- r  w" L4 iMariaDB [tripsql]> exit
9 y/ [2 `, K- G5 {Bye3 O1 D" Y" N0 i& M% T

" X( V" C; ]9 q9 U5 M# y(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order% B+ g8 h9 I1 ~! h! Q3 x- H
Operations to perform:9 q3 T$ G# f0 @. \$ B2 {$ D3 T% v3 H
  Apply all migrations: order, g/ E! s7 _. q7 l" W" {
Running migrations:
" `7 v8 Y( n  v" \  m3 w  Applying order.0001_initial... OK0 k6 j: e: h: H5 K0 W* e6 |

& {, q9 E9 O% M/ Z0 X(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>7 T0 z: p/ y( B& _* i5 @& k
这次成功同步过去了。$ U! \* m6 ]4 {( I9 s  ]: b6 a

* J5 k6 R1 L) ?, p. z2 F! ]
1 M8 B* }* ]% x' P: W, V4 [8 y( D0 L$ @2 O- t3 e

! Q' p" q: S8 z6 Q4 O
7 ~" b! r/ K) n, d, Y5 ]" n1 z- i" p; A5 k. _' O

+ `0 V& D( V: }# l1 W; ]
8 u7 `0 u/ ]. m& N* q(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero
. p0 m9 E1 _8 a% R6 o4 XOperations to perform:
/ V; m4 z; Y; X; H5 S5 J6 c  Unapply all migrations: order* Y- d9 c; P6 s" q* [' c. y
Running migrations:
3 R# w. I: j7 f( H  Rendering model states... DONE1 `! R0 @: m! f7 p3 f! K5 |
  Unapplying order.0001_initial... OK
4 d* t9 h4 s- V4 ?% ?
# E. |5 j, A6 A1 D/ s; C(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero
+ A# k" y/ V7 }% B( Q! F6 NOperations to perform:6 Q! d- |6 Q9 g# a" ]/ d; O) V
  Unapply all migrations: order5 W$ Q3 ?2 T7 z( {$ H3 Z/ s
Running migrations:
; z" Q/ s4 o5 C# N$ |  No migrations to apply.  ^9 q# w0 z% [& S% u+ B

# a' ~9 A/ ?0 K* F9 {(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations --empty order% y" M3 N$ {% ^. i4 B
Migrations for 'order':
/ m2 F% z0 E% t  order\migrations\0002_auto_20250718_1752.py' n) L4 }- ]! P3 ]3 p& p! M5 l
0 c) @$ F0 n# m( s; B; Z$ n
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell
1 X- C/ ~8 g* n- n& U/ Z' LWelcome to the MariaDB monitor.  Commands end with ; or \g.
* f+ Q8 ~: I% RYour MariaDB connection id is 162
. F9 s! L/ _6 \& l0 R* Q2 OServer version: 11.5.1-MariaDB mariadb.org binary distribution
4 T3 S1 W9 G* F, b* R1 k/ O+ O# c& D4 n- H2 d# [$ {% p  B
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others./ V' @2 h% ?/ M

& I* f, B1 f# Q3 ]Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.2 |+ {, Y1 F; m# \* B% ]

! {0 s4 z: N" g, LMariaDB [tripsql]> delete from django_migrations where app='order';
. g4 M4 S( W$ r7 V. R$ uQuery OK, 0 rows affected (0.001 sec)
$ k' }# Q9 q! F* c4 A2 d# Z8 M" a/ O' K( \
MariaDB [tripsql]>( N: G, E8 P; I- N
+ Q; Y. u/ F# }
2 l% Q* t$ L, W
- U* F% y. U! Q- e$ H. T
6 P. f* z1 `2 s9 c1 p2 Q
0 K: U2 J% ^( s$ s; o0 y8 X) y5 _5 o

  d8 `/ L$ [* Z4 N; o* k

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-7-18 17:22:08 | 显示全部楼层
问题描述:
4 f+ Y1 o3 u% w: ], d9 \已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrations确认成功,执行python manage.py migrate,提示No migrations to apply. 表示一脸懵逼。再次修改,指定表名,再次尝试,发现问题依旧,表示二脸懵逼5 }; `6 ^; p( _1 B. N8 l' v8 A2 d' b+ a

' K) Z- ?' }' w排查过程
; _2 p3 s5 V/ H' Dpython manage.py dbshell 进到数据库里面,查看是否表已存在 * ?1 M; d0 q! z  e, ~
结果:表不存在; g7 n5 u" [0 x+ U0 d
检查migrations文件 1 J$ ~. n5 ?1 U2 g
结果:文件没问题& J# x* W4 O9 j8 L5 |5 P
百度 google 各种搜,乱投医,各种尝试) R- L  ^# l3 y. n
解决方案
) l9 t- w5 J8 T; u/ Z$ Z5 R- rpython manage.py dbshell 进到数据库中,执行delete from django_migrations where app='your_appname';9 s, T: N9 N6 Z1 N
python manage.py makemigrations(若migrations文件未删除,可不执行这一步)/ T9 t$ Q" A6 l& N/ i, Y- o; t
python manage.py migrate 好啦,大功告成) @$ @5 o% f" L7 {# c5 P
原因分析
( G4 `8 u% M7 H: Q/ R0 L查看django_migrations表结构 9 v; L. ^. h9 U4 ]
建表语句:
$ a' q7 E3 _5 y  B) oCREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL);
" b" ]; m7 |! G1 V; ~1 M3 I原因 ! H7 e  L% _% @. m" _$ p
造成多次应用migrations失败的原因是,当前model是修改过的,原来的migrations已经被我删除,但是,重新生成的migrations使用递增整数记名,所以,在django_migrations表中0001,0002等前面几个数字的文件都已被记录,在Django看来,被记录了就相当于已应用,所以,会出现刚开始的No migrations to apply.
& i& {- A% p# w, t( n1 E/ d避免方案
- U0 ?- K( t8 ?% i& a有强迫症删除migrations文件的同学(比如我),请同时到数据库中删除相应记录: m; f. K7 b( U) x* p
没有强迫症的同学,可以继续生成新的migrations,旧的就不必理会了8 M& l! f! V% I) f. |3 o+ d1 O
题外话: p8 K4 V! S) r, x3 I3 X  {
执行python manage.py migrate之后,可以使用python manage.py sqlmigrate appname migrations_num(例如python manage.py sqlmigrate user 0002)查看当前migrations文件对应的sql语句。
6 r. @- V) |( |  A# S* 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;
' N! W: [3 B+ ^" ?$ s; A+-------------------------------+
1 r$ t; \" ^% U" N| Tables_in_loogcn_data         |
  X; [! y5 E: ~1 v& I6 c+-------------------------------+& Z/ C) C! a4 s. N, p0 ?
| account_user                  |
, E; f+ W6 u1 H# k7 ^| account_user_groups           |
# B: s$ M; ^9 \, h| account_user_social           |
, Q$ X" r  o4 a& K| account_user_user_permissions |4 ^9 Z4 E4 x$ G2 u& G2 C
| auth_group                    |: N* t4 R" [! M0 x$ I" F& p! e
| auth_group_permissions        |2 n! h2 {/ w& }5 w( ]
| auth_permission               |
9 r+ j% @, b' t$ }0 j; Q, X, || django_admin_log              |
* P# Q8 |6 n3 ?& h" ~$ }- z) A9 h, H| django_content_type           |
9 ~% c+ Q  w4 G9 w! r1 X4 t| django_migrations             |
! c1 D/ P* S7 t8 f: n| django_session                |' h2 {: n# w3 E7 V& e) i: b- R9 `
+-------------------------------+
: A0 \( l/ I  m8 @7 A11 rows in set (0.001 sec)1 W2 R  u. h$ e& F1 v; [
# [, \- t1 S, f: ]" f
MariaDB [loogcn_data]> select * from django_migrations;
% W) k, T; J, a* R( T# m+----+--------------+------------------------------------------+----------------------------+* j# b2 ]- p$ z! ?
| id | app          | name                                     | applied                    |
- Z; `, r* D$ m; m. q& R( t+----+--------------+------------------------------------------+----------------------------+
! j( u4 Q, W; W0 F|  1 | contenttypes | 0001_initial                             | 2026-05-23 02:00:34.278182 |' c- n0 j0 {& p4 Z. i
|  2 | contenttypes | 0002_remove_content_type_name            | 2026-05-23 02:00:34.505337 |
3 u$ m, O  t5 Z+ a! R) W# G  ^1 I8 h|  3 | auth         | 0001_initial                             | 2026-05-23 02:00:35.411267 |
% p& \* J6 H$ d$ \+ a# @# u|  4 | auth         | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |& t. y1 |& h+ ^5 a
|  5 | auth         | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |3 a: \: m2 V; O7 w. r" }& g! N
|  6 | auth         | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |
, \  p5 ~# i+ b- _2 w: W|  7 | auth         | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |
: m) G7 q' R- E|  8 | auth         | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |. L( P* g: f4 K8 B9 E+ t( l
|  9 | auth         | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |
/ u$ d, P( q; F/ T! I, i| 10 | auth         | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |$ ]# X/ n" b) Z4 {
| 11 | auth         | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |/ q0 M  b- [7 S  J0 _; }1 z
| 12 | auth         | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |
* K# Z# ]) l2 b) m5 _7 F: c, ?2 ]| 13 | auth         | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |
6 w0 w/ F; b; Y5 f  K" p/ T* u| 14 | auth         | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |  [0 l" `2 {7 D7 j
| 15 | accounts     | 0001_initial                             | 2026-05-23 02:00:37.445274 |6 H4 z+ U; R6 D3 w2 O3 L; `( [2 L
| 16 | admin        | 0001_initial                             | 2026-05-23 02:00:37.817254 |) ^& w: U% f; V
| 17 | admin        | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |  G/ H, l+ y* \3 C
| 18 | admin        | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |
9 r5 W% W. |8 G, f& q: h| 19 | sessions     | 0001_initial                             | 2026-05-23 02:00:37.991277 |3 @, \8 a+ R5 g1 e2 k& X' ^
+----+--------------+------------------------------------------+----------------------------+
! Q2 i! F1 W( Z* q% P: a19 rows in set (0.001 sec)
: G! v4 r% g3 A  c% H
* D0 j- ~' Z( {9 R6 B4 v2 C) @MariaDB [loogcn_data]> delete from django_migrations where app='accounts';6 K- U/ K1 T9 p- B# t
Query OK, 1 row affected (0.001 sec)5 O! E9 R  k, e
( F6 ?8 R5 h6 w( V, ~/ {3 u
MariaDB [loogcn_data]> delete from django_migrations where app='contenttypes';
3 V. H% E5 T" i  bQuery OK, 2 rows affected (0.000 sec)5 ~7 H/ L# p$ Z/ X5 O

- S% s9 U5 p, }8 ~MariaDB [loogcn_data]> delete from django_migrations where app='contenttypes ';
: _; H6 d! O& z3 T! eQuery OK, 0 rows affected (0.000 sec)3 ~0 o- c& f+ j/ U3 t! `/ D5 b
7 G$ q! ?- n0 H* ]  G  Z
MariaDB [loogcn_data]> select * from django_migrations;
) M0 e4 o7 a* V* ^) |+----+----------+------------------------------------------+----------------------------+0 Y& j7 |* d; `6 |. d6 x
| id | app      | name                                     | applied                    |+ _% n0 u5 ~2 K+ C) f7 X* k
+----+----------+------------------------------------------+----------------------------+& [# h7 p+ Z1 @- O/ S4 m6 W
|  3 | auth     | 0001_initial                             | 2026-05-23 02:00:35.411267 |, W  M' m* i; Z4 l
|  4 | auth     | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |
5 K! Q; w# |1 H8 q  G|  5 | auth     | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |
! c, j0 U% h' p* t+ T|  6 | auth     | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |3 t8 s2 D5 u8 E1 ~" |
|  7 | auth     | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |
$ Y( k' L9 G) ?6 L9 h|  8 | auth     | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |
. n  n: F# i( m9 n|  9 | auth     | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |% @) B! \1 S4 ]4 H
| 10 | auth     | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |
2 L# J0 _% O) I9 a' p| 11 | auth     | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |
/ g3 n6 u. D" x5 s| 12 | auth     | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |
' B% A/ u# u) D, r9 d( E0 E2 ]( J| 13 | auth     | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |
: J7 L) X5 n1 R8 F# `# X| 14 | auth     | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |
+ |8 d1 n& B+ r7 X; f| 16 | admin    | 0001_initial                             | 2026-05-23 02:00:37.817254 |
! j8 M4 x: O0 P: R9 t! s| 17 | admin    | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
2 p, o0 b% K" W/ k| 18 | admin    | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |6 ]) H7 t, X3 E. ]: o1 `4 k1 b  \$ ~
| 19 | sessions | 0001_initial                             | 2026-05-23 02:00:37.991277 |/ [- t) a/ L2 l* ~
+----+----------+------------------------------------------+----------------------------+
9 z' O: ~+ M% d! r! U; F* x. g16 rows in set (0.000 sec)1 K# e( W) ~* z7 W1 M

8 u5 U1 L4 e* i: KMariaDB [loogcn_data]> delete from django_migrations where app=' sessions ';
  @4 e, m+ Y7 x& M" t2 V' X6 u: IQuery OK, 0 rows affected (0.001 sec)! B( Z# x8 _  B+ t: G! c; v: z
4 A% D5 n6 U; q
MariaDB [loogcn_data]> delete from django_migrations where app='auth';
0 J3 ^  E2 H2 m0 ^+ v' W$ gQuery OK, 12 rows affected (0.001 sec)
- q& `& l( W5 I! o% m! M+ N6 _5 P! G# T9 N9 ~$ b6 d! }9 O
MariaDB [loogcn_data]> delete from django_migrations where app='auth' and app='admin';
, _) T3 w( `- [' q/ T- |1 @: }) ]/ pQuery OK, 0 rows affected (0.001 sec)
5 \3 A* j. w9 F$ j9 t$ I6 t) J% D# ^! i  X5 K" U5 }% u' Y+ E
MariaDB [loogcn_data]> select * from django_migrations;. G# M9 \8 i6 n0 i) S  C8 \) `, q* z
+----+----------+---------------------------------------+----------------------------+2 I. i' }( N# o7 g$ \
| id | app      | name                                  | applied                    |9 q2 v, v' \9 }( j- |8 U
+----+----------+---------------------------------------+----------------------------+
& T- @! h( ]! Q  ?9 W& J, l| 16 | admin    | 0001_initial                          | 2026-05-23 02:00:37.817254 |+ ]* x2 {" l/ A: s3 b/ O
| 17 | admin    | 0002_logentry_remove_auto_add         | 2026-05-23 02:00:37.827664 |
3 f; r6 q) X; T8 J+ p( v( n| 18 | admin    | 0003_logentry_add_action_flag_choices | 2026-05-23 02:00:37.836978 |1 h/ V: t) g. ?& O' x, S
| 19 | sessions | 0001_initial                          | 2026-05-23 02:00:37.991277 |' T, @+ O" R( J2 }
+----+----------+---------------------------------------+----------------------------+
7 g" `- i3 A2 u0 p4 rows in set (0.001 sec). J+ P' J6 A  ^* P& l" d2 Q. D

+ V, {6 S% P$ w+ P. H, B2 F3 q1 E, g4 k/ pMariaDB [loogcn_data]> delete from django_migrations where  app='admin';; [) _' l6 Q3 ^7 k
Query OK, 3 rows affected (0.000 sec)& t: @, f( `- K8 j# t6 J' G6 d8 P  N

! V% {; T/ Z' J7 J) f" \4 V/ XMariaDB [loogcn_data]> select * from django_migrations;/ g5 O6 s& P. u
+----+----------+--------------+----------------------------+; u7 C) Q6 P' C% R8 M4 R' S9 ^% s
| id | app      | name         | applied                    |
* s' w% y: [( m( D4 z2 a+----+----------+--------------+----------------------------+
/ x' ~7 ?* W3 n; {2 |  c( z| 19 | sessions | 0001_initial | 2026-05-23 02:00:37.991277 |
# u5 e1 c% Q' A# D5 b7 E# o+----+----------+--------------+----------------------------+, g. G; y. ]" [2 x
1 row in set (0.000 sec)
, I7 R" |9 M& J6 j) ~
+ H# h, y2 ~6 k1 x( b9 t8 Z( @3 t. uMariaDB [loogcn_data]> delete from django_migrations where  app='sessions';( e# j2 ?0 q5 D" P+ X# l' \# B
Query OK, 1 row affected (0.001 sec)
, e; u; u$ L; q6 R+ X" B
6 l6 K5 |) ~9 l1 [1 zMariaDB [loogcn_data]>+ c# f& Y6 ~9 G1 d+ b
清空所有的表,; E5 C0 j! T% Q" a( A/ }1 g

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:27:29 | 显示全部楼层
进入 MySQL 终端,删掉当前数据库并重建:sqlDROP DATABASE your_database_name;
1 a2 G$ M% [5 e1 GCREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
$ s3 |  {+ S' e5 V: F' h请谨慎使用此类代码。重新在终端执行迁移命令:bashpython manage.py migrate
! @0 Q; R( R4 g& I9 l3 ^7 W请谨慎使用此类代码。如果你使用了方案一或方案二后遇到了其他报错,请告诉我:这是新创建的项目还是维护中的老项目?数据库里是否有需要保留的业务数据?我会为你提供更精准的修复步骤。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:33:03 | 显示全部楼层
MariaDB [loogcn_data]> show tables;7 h  Z5 o/ S- \  G8 A2 L6 Z6 e* ~9 t5 C
+------------------------+
8 k; I8 N' ^! H- h* Z| Tables_in_loogcn_data  |/ U4 L4 y: T+ P% @+ P1 ]) @
+------------------------+
* c! u9 g: |% A$ v' X( U0 B| account_user           |* z) _# P9 g3 `( \+ t; R: \7 j5 ?
| auth_group             |# K! h! m' |+ E" }2 O
| auth_group_permissions |4 \) D2 k) T. \( m! `
| auth_permission        |! h* t/ X; }1 ?. G' u
| django_admin_log       |2 K7 I! G9 r7 }9 O- D) w( A/ @$ S
| django_content_type    |" t9 _) [9 j$ ]5 [/ w
| django_migrations      |
+ N* [8 r0 U6 ]8 @0 m| django_session         |. t' Z; ]/ Q) d  ^% |
+------------------------+
' s0 i/ s+ q% Z2 A. J% n" o) Y+ O+ l8 rows in set (0.001 sec)3 d" `) c/ P# [* Z- R  H& V
2 e! U# ^: Q# y' k5 e) O" A, P9 y
MariaDB [loogcn_data]> drop table if exists auth_group_permissions;
+ _# b3 o. j0 H/ }* W+ v  oQuery OK, 0 rows affected (0.058 sec)
* W  }( u, y; O7 u
% G* J& ~+ r) J, y5 T- QMariaDB [loogcn_data]> drop table if exists django_admin_log;9 A1 c# m2 W/ l, C6 N
Query OK, 0 rows affected (0.056 sec)4 @0 w' ]5 C7 ^# j/ V, k
1 `$ Q  W8 H7 [8 r: X: h# B! n
MariaDB [loogcn_data]> drop table if exists auth_permission ;
3 D, l* X6 e+ G1 {1 U5 f6 A3 vQuery OK, 0 rows affected (0.061 sec)
0 [* v6 L7 i$ w/ s4 @3 L- b; ^1 Q. v. D4 B; s; w' F$ ~# k
MariaDB [loogcn_data]> drop table if exists account_user ;
5 L7 o9 u) m/ ~5 yQuery OK, 0 rows affected (0.060 sec)6 O' f# p- P2 F- f2 L7 u% F
( T) g8 a; Q8 K/ {% D! ]( [2 u$ Y
MariaDB [loogcn_data]> drop table if exists  auth_group ;
3 u" N: b* i3 F' k+ b3 QQuery OK, 0 rows affected (0.055 sec)2 B- J4 R9 O- C4 g+ @

2 E& L. j' b/ k% s: L( O+ iMariaDB [loogcn_data]> drop table if exists  django_session;
( O: }9 r% G- K- x5 CQuery OK, 0 rows affected (0.051 sec)
2 m" i/ A- [1 c' o# a
) G$ C: U$ K& K3 ]8 VMariaDB [loogcn_data]> drop table if exists django_content_type;
: ]; T) ]* S- U# J) WQuery OK, 0 rows affected (0.052 sec)3 Z: q. a% V( H2 v$ E( p: V; z
' x" V0 e. C, A. m. N! b2 M
MariaDB [loogcn_data]> drop table if exists django_migrations ;
% e5 M0 N  l; Y/ D- wQuery OK, 0 rows affected (0.068 sec), [4 T2 b; a; E& p; ^4 x
1 I2 ~$ q# D* l& p3 n$ U+ u
MariaDB [loogcn_data]> show tables;7 B, h& I( x& y; t" D! l
Empty set (0.001 sec)
" ^3 A1 B; e) c1 q2 b% x, `7 V
5 Y2 Q: p  r) U$ {7 k- K' M+ |8 iMariaDB [loogcn_data]>/ u* H- Q7 q! a3 L2 R: A/ V
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-11 23:14 , Processed in 0.026289 second(s), 24 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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