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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2025-7-18 17:17:02 | 显示全部楼层 |阅读模式
因调试过程中,发现有点问题,就把数据库表从其他客户端删除了,以为删除了就可以同步过去,没想到就是下面的结果。
; r1 \% C0 t+ J' D; u) T- R3 a(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py check8 v3 n: C% _; H+ t; R/ m- u
System check identified no issues (0 silenced).
. u6 ]4 J% `/ e# Z8 {$ P3 Y6 l, ^9 m# a9 K" m& p6 x
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations  o% n* U. Y  {4 s3 A8 k: W
No changes detected+ a6 g4 ~6 K% F/ `
; L0 Y9 d6 {% ^/ E, }' N! Q. ^" h
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations order! B: u, G$ M0 C3 T: g$ G* e
No changes detected in app 'order'
9 E6 E; D  d5 j0 @# D! I0 \  C# \3 ]7 O. Y/ a
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order6 I' x4 X6 Q3 Y9 G4 a" O9 \" E
Operations to perform:
' q( O1 v& h* P3 O8 p  Apply all migrations: order  J7 A6 I/ i7 w/ W
Running migrations:+ z) m$ m6 }+ O4 e1 ?' L
  No migrations to apply.
+ {" K* c% e" F7 O1 Y/ R$ F8 f6 a0 [. V# h
同步不过去,怎么办?
* C) p- G6 n; @1 c9 O看到有高手的同步经历,按照他的方法,去执行了下,好像问题解决了。
3 x: K, X+ z* o" h; x& G, ~3 h# U2 e9 s& q4 n
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell
. B) r) B0 J1 v8 H0 yWelcome to the MariaDB monitor.  Commands end with ; or \g.
* U, V, h9 s* u( e% k% y7 \6 cYour MariaDB connection id is 1527 Q7 B( j$ C- G$ V: `
Server version: 11.5.1-MariaDB mariadb.org binary distribution* B  n( e  M! r  p
7 }1 x7 ?! {; R# E( T9 v
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
% [% d  s% F# |, o6 _- e# e) h! p
8 e( p( \: U' N" S: ]Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.4 ^) d3 r/ m9 ]9 Y* D5 k
/ K% y8 {9 Q  o, c! e
MariaDB [tripsql]> show tables;7 m8 k) z: o( O) G  y+ I3 S$ h# [
+-------------------------------+& n4 K' [6 R& q; }+ a9 u
| Tables_in_tripsql             |
- q! ^1 T" b2 _' g6 I+-------------------------------+
- x- Z3 E. D  _1 c2 [+ Z| account_user                  |- I* a/ m6 O( `7 I. z$ x
| account_user_groups           |
2 R" X( P0 _. ~9 P| account_user_user_permissions |
) I8 j; [/ }* [3 S+ r2 v$ N' n# z| accounts_login_record         |
2 y( d; h& v/ q9 z# G. K: h| accounts_user_profile         |- e& `2 L+ N( v% U5 i7 Y
| auth_group                    |& ?6 }/ k1 o  j+ X6 v* W
| auth_group_permissions        |% {" ?5 j- b4 ?$ U4 O2 m
| auth_permission               |
9 }! E) S$ j) ?/ u3 j| auth_user                     |4 K9 q7 m' L/ ?9 N/ w
| auth_user_groups              |
" H, x, v* }/ B; j- t) O2 \; d+ y| auth_user_user_permissions    |
; F6 Q; L" [5 t; W+ V" v5 ^8 Z" G| django_admin_log              |( h* a( ]$ d: D, p  A) S5 H, J3 I
| django_content_type           |
- ]& S9 r4 ~! M| django_migrations             |( _* o% m! a; y+ u
| django_session                |9 ?. D, U; w- U" F# g* L# L$ f
| sight                         |- p4 p' ]% Q& `! }4 S
| sight_comment                 |# L" e9 A% A3 D+ s8 Z- G7 Q3 n
| sight_info                    |: w+ K8 l' y. }2 Z: `% ?
| sight_ticket                  |
( L, C4 M( A. F. ^) v) c" j' K/ x| system_image_related          |- ~! h4 B) z  F+ [
| system_slider                 |1 |+ I. G% e9 j& {- r
+-------------------------------+
" C5 e+ ]6 U  ?, R& d& x- A21 rows in set (0.002 sec)7 p' C% _5 W/ y0 D" K0 g: e
  j1 P" N  o* B# [( d$ {
% I. v$ \# |; Q, e! T) ~
MariaDB [tripsql]> delete from django_migrations where app='order';9 L* [/ b/ Q- c+ n, ]$ H
Query OK, 2 rows affected (0.004 sec)
4 r! z" t$ f! k
. P2 ?* y) F; y0 C! }MariaDB [tripsql]> exit
$ w  N7 M) ^- R- h% nBye& ]/ e" J) J- p) v7 h
5 W) d( e( r+ U' b2 X  G  \
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order
/ E" ?, Q+ h5 m5 w: g+ iOperations to perform:
  B9 H$ ^$ Z# F- ]9 {/ U3 a# x2 K  Apply all migrations: order' G/ d5 i2 I, ^
Running migrations:
8 h+ @5 w+ C  z+ ~0 [$ i: k, L( x" d  Applying order.0001_initial... OK
7 G3 H# C2 p7 x' r& B
% q9 [" c5 @) b8 a: L+ y+ |(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>& [1 e8 ~; J4 a! [0 O' n, \
这次成功同步过去了。
- h: D: J$ Q+ K/ V4 n
/ u! [+ U  E4 n$ i& z/ F# s- b9 e* O& q) I& c5 E* W- ]; Q
( S9 n( W2 P" i! X; u
) F9 F9 \4 j9 @$ n5 V
2 p1 o. }5 l0 i" [& {0 P

  c/ M1 E$ x8 {4 [0 ^  o6 h: q
* n3 _0 j% `: ?' H
% A' B. E2 l/ r$ Y- i4 V(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero
- U1 V, a# {. w& W. zOperations to perform:
( e6 A/ \7 M' _3 v, s' V! q  Unapply all migrations: order
8 q( W1 N! ~5 j" GRunning migrations:
6 |( O2 l- ?+ I: U5 L1 C  Rendering model states... DONE
- }# Y! o9 Z0 r. T0 }) t4 j* O  Unapplying order.0001_initial... OK# e3 e$ @  g' V; [, m
1 s" j7 K' ]& ?$ C
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero
, v: Q! L. t. p: o  oOperations to perform:. h* R8 p1 U- w  J7 T; W, R. m5 }
  Unapply all migrations: order
& p9 |2 ?* B: W) t- F/ mRunning migrations:
! H3 v, a, t! p7 p8 G. r  ^# @0 R. r  No migrations to apply.
9 V5 k% Z( `+ P& }
, X1 N- h6 t5 g0 C- k$ ?(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations --empty order
9 ?1 J& z7 K0 T% L# TMigrations for 'order':
! {, B& @5 e5 U& _; B/ d  order\migrations\0002_auto_20250718_1752.py
$ ?/ F- \" G% U7 J! t* m& @' t" L
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell
! W/ |  |6 K; `6 x/ _Welcome to the MariaDB monitor.  Commands end with ; or \g.& ~! C' f* R  a( ?7 ^1 b
Your MariaDB connection id is 162& \& J( b9 D* f  e& Y, _
Server version: 11.5.1-MariaDB mariadb.org binary distribution/ _  k1 T, ]  g: Q+ C, v

( W  J* H# [( u3 _+ [$ S: qCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
2 T: v* a8 u3 z# R( R; M3 m1 E
; E1 G1 V  J5 x% c1 `- fType 'help;' or '\h' for help. Type '\c' to clear the current input statement.
5 E2 j+ X' |, Y4 W( i# U6 \
; C# ^9 J. W( B9 }. O3 sMariaDB [tripsql]> delete from django_migrations where app='order';
3 B5 c- E: Y  l  fQuery OK, 0 rows affected (0.001 sec)
( I4 o9 O$ R0 c* |1 u6 X2 |9 z8 A2 g: ~+ k! q2 B
MariaDB [tripsql]>
$ h3 W# O4 r; e7 W
" @* G8 |2 O- W- [2 L  @* r1 q# e6 _: k5 B/ }* ~. S4 a# f

/ d( i( t) }$ U: W% E  r
0 {$ m( t, @# K8 K1 e- Q+ m6 P- e* H6 j

: e4 ~7 O' U8 N* I% o# [9 K

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2025-7-18 17:22:08 | 显示全部楼层
问题描述:
: V# F: ^$ ?2 q3 P3 C. R已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrations确认成功,执行python manage.py migrate,提示No migrations to apply. 表示一脸懵逼。再次修改,指定表名,再次尝试,发现问题依旧,表示二脸懵逼
7 m6 s# @1 t: I' T3 }* ]. d' d
9 x9 Q5 _# x. n排查过程/ U0 {+ z0 ^3 \. \) F6 C" D
python manage.py dbshell 进到数据库里面,查看是否表已存在 3 ^; j2 a! h6 Y6 _
结果:表不存在9 J) _  [& D8 c) G- V
检查migrations文件 5 l# ?$ r- y' d. `
结果:文件没问题
2 L! s8 N! v* ]百度 google 各种搜,乱投医,各种尝试
( n+ m: d( [: j3 h解决方案+ y. Z  e' L( k1 q- U8 d+ m  D
python manage.py dbshell 进到数据库中,执行delete from django_migrations where app='your_appname';
6 u6 G4 G* i  [# j) w" Ypython manage.py makemigrations(若migrations文件未删除,可不执行这一步)" y+ g& \; \6 ?# A/ u" E% P
python manage.py migrate 好啦,大功告成
- M) L3 ^; \& d5 P9 P; D原因分析$ z( I! b7 ?) V/ |4 ?* C2 |7 p& C
查看django_migrations表结构
% t4 t+ z7 m& T建表语句:
2 Y( Q9 E1 W9 |% [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);
) ~" W# V" u( m  Q) O8 Z' r原因
' W/ O# G  A0 d7 E造成多次应用migrations失败的原因是,当前model是修改过的,原来的migrations已经被我删除,但是,重新生成的migrations使用递增整数记名,所以,在django_migrations表中0001,0002等前面几个数字的文件都已被记录,在Django看来,被记录了就相当于已应用,所以,会出现刚开始的No migrations to apply.  S2 a+ C$ c# b" H0 a) G2 Z4 X
避免方案9 M  m8 v9 q! G0 N7 r
有强迫症删除migrations文件的同学(比如我),请同时到数据库中删除相应记录
+ C# I0 I- D3 [1 h# B. [: P) h没有强迫症的同学,可以继续生成新的migrations,旧的就不必理会了
$ T7 Z1 [0 \5 _" u$ u$ [, L! y题外话+ P) V8 @' p* _4 M& S4 c$ Z/ T0 Q' C6 l
执行python manage.py migrate之后,可以使用python manage.py sqlmigrate appname migrations_num(例如python manage.py sqlmigrate user 0002)查看当前migrations文件对应的sql语句。
. z; J# A$ y+ G5 @6 e  V. N, x) H3 \另外,在使用上述命令查看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/ E; J/ L; x) x" ^0 i, Q
+-------------------------------+% e* J" i* E7 v
| Tables_in_loogcn_data         |6 |9 [' T2 f  C% ~2 J
+-------------------------------+* ?8 L% W6 Y  N) _& g7 f0 W
| account_user                  |  l# `$ ]  b" ]
| account_user_groups           |  S# n9 r& A2 |' g
| account_user_social           |
+ x3 `) ^4 c) Y+ O$ X0 L. S| account_user_user_permissions |
- K* w. y. ?5 O' F' l3 }/ j! Z| auth_group                    |
3 L" T# U/ _5 f/ ~5 f| auth_group_permissions        |
* Q+ Y% \- f' g5 l| auth_permission               |, H6 E6 D1 m  @/ G6 m
| django_admin_log              |- A0 _5 g. X; y9 A
| django_content_type           |
* W0 F4 o( V: U8 D| django_migrations             |
9 f0 `. j" N+ S$ E8 q7 E: D& _1 D| django_session                |* g& @6 N) E- u5 n3 ]) @- [
+-------------------------------+% B' g2 {" Z# m8 c3 f/ }- Z
11 rows in set (0.001 sec)
' t' q! l1 _# m  L( k1 [9 I
  e3 z8 ^1 V/ l! |$ ^5 rMariaDB [loogcn_data]> select * from django_migrations;
1 S3 l' k- F( }- o# ]7 ?6 N+----+--------------+------------------------------------------+----------------------------+5 y+ |% w# T1 m4 W, T
| id | app          | name                                     | applied                    |7 b9 I, T/ L  Z* N
+----+--------------+------------------------------------------+----------------------------+- v+ m2 Q, {; L2 T! j5 J- |
|  1 | contenttypes | 0001_initial                             | 2026-05-23 02:00:34.278182 |
" d- `0 e! c8 S/ T6 h# s6 |: s8 M- z|  2 | contenttypes | 0002_remove_content_type_name            | 2026-05-23 02:00:34.505337 |
% Y# ^' W2 e) `: u+ ^|  3 | auth         | 0001_initial                             | 2026-05-23 02:00:35.411267 |! f" n( `$ E7 {2 B
|  4 | auth         | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |
4 [! J: E/ Z8 z3 X) e: g8 L|  5 | auth         | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |, X5 n' {' w. S
|  6 | auth         | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |$ N- A1 A, m0 o' J/ A7 B1 T
|  7 | auth         | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |
$ [: j- ?1 s' K|  8 | auth         | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |
; Y8 i$ L4 ?) z4 Q. M1 v|  9 | auth         | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |
2 }8 |0 j! G& K| 10 | auth         | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |
  @# A$ B2 s! @$ D2 R7 e| 11 | auth         | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |
, Z$ r; G) G8 ~. S/ T5 m* S+ [) {  b| 12 | auth         | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |
5 e7 @, W" x% A| 13 | auth         | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |
$ `; a7 {4 c& p6 ?& E/ k% {* e" ?6 `| 14 | auth         | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |
3 P; X, T) r- `| 15 | accounts     | 0001_initial                             | 2026-05-23 02:00:37.445274 |% G+ Q: t  [, p) k" I; |
| 16 | admin        | 0001_initial                             | 2026-05-23 02:00:37.817254 |
- ~$ n# P8 U+ e+ _, m* ~$ i| 17 | admin        | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
! T: ^  \% h" F# Q+ g# y% b| 18 | admin        | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |+ D' P0 ]" H, V. R: p
| 19 | sessions     | 0001_initial                             | 2026-05-23 02:00:37.991277 |: d- A; S9 N" k
+----+--------------+------------------------------------------+----------------------------+
+ Q1 H2 @  l* e( ]& g, X; Q19 rows in set (0.001 sec)
  _& H# n1 R; f$ F0 p" r7 V1 g  V; I$ P: j4 I+ n& H, x+ ~
MariaDB [loogcn_data]> delete from django_migrations where app='accounts';
; Z) F; y/ y9 s+ c+ I* nQuery OK, 1 row affected (0.001 sec)
' y+ x" E7 }, J3 `! l! ~5 z: }+ U2 ^0 F* \; ]
MariaDB [loogcn_data]> delete from django_migrations where app='contenttypes';
! p5 m! k2 _' U; c* O( a( sQuery OK, 2 rows affected (0.000 sec)0 U( ?4 K* a( a% r

' `/ ^& [  m1 C* Y9 iMariaDB [loogcn_data]> delete from django_migrations where app='contenttypes ';
( b& y0 i/ B, p3 p3 O5 d  WQuery OK, 0 rows affected (0.000 sec)' t, ^+ b$ g2 B# j, N6 H5 ]/ ~

6 G* M& R8 ]; W' w9 @MariaDB [loogcn_data]> select * from django_migrations;0 ^0 P& h' V1 ]& k6 T- o6 `
+----+----------+------------------------------------------+----------------------------+: g4 \' V" G! s% l
| id | app      | name                                     | applied                    |) L' W- ^. d! J& W
+----+----------+------------------------------------------+----------------------------+# S$ g# T' R4 V' p, a) y
|  3 | auth     | 0001_initial                             | 2026-05-23 02:00:35.411267 |
, e) g! m' L" b. n4 l/ S|  4 | auth     | 0002_alter_permission_name_max_length    | 2026-05-23 02:00:35.567344 |
' m/ {, Y) s0 z7 R|  5 | auth     | 0003_alter_user_email_max_length         | 2026-05-23 02:00:35.573367 |& k1 q) C) P3 z% P* h
|  6 | auth     | 0004_alter_user_username_opts            | 2026-05-23 02:00:35.578785 |2 K' M* A; _0 f$ {& A+ B. E! z
|  7 | auth     | 0005_alter_user_last_login_null          | 2026-05-23 02:00:35.584691 |, m6 P2 p, n7 U, p2 }; U& }
|  8 | auth     | 0006_require_contenttypes_0002           | 2026-05-23 02:00:35.585385 |
. o( A, Q' C& P( @: E) o( d|  9 | auth     | 0007_alter_validators_add_error_messages | 2026-05-23 02:00:35.590374 |3 X4 D" u6 H# t8 J3 ~2 P  j
| 10 | auth     | 0008_alter_user_username_max_length      | 2026-05-23 02:00:35.595604 |: G3 M8 m8 O3 k5 G5 r2 L: E" G
| 11 | auth     | 0009_alter_user_last_name_max_length     | 2026-05-23 02:00:35.605769 |) B2 x' r1 ~7 O; y
| 12 | auth     | 0010_alter_group_name_max_length         | 2026-05-23 02:00:35.705389 |
9 Y1 \0 N, q( J8 C| 13 | auth     | 0011_update_proxy_permissions            | 2026-05-23 02:00:35.711556 |+ R" Z; I0 F6 ?0 w
| 14 | auth     | 0012_alter_user_first_name_max_length    | 2026-05-23 02:00:35.716632 |) g, s8 k6 J6 _: ]6 L! f& D* g* e! r
| 16 | admin    | 0001_initial                             | 2026-05-23 02:00:37.817254 |
. T8 M3 W  {3 o| 17 | admin    | 0002_logentry_remove_auto_add            | 2026-05-23 02:00:37.827664 |
4 @0 n6 \  S, _9 v: \1 {| 18 | admin    | 0003_logentry_add_action_flag_choices    | 2026-05-23 02:00:37.836978 |$ K% V, P; _& I5 p2 D: s
| 19 | sessions | 0001_initial                             | 2026-05-23 02:00:37.991277 |
' ?; c/ ~( g+ M; k0 V. R' E+----+----------+------------------------------------------+----------------------------+" j; Q2 w/ s: K  S
16 rows in set (0.000 sec)
3 y8 c4 c* |) p9 [4 A
. x& N# P  [" K- o. Z% CMariaDB [loogcn_data]> delete from django_migrations where app=' sessions ';" r2 G% ?3 E$ v* P/ j  S& j, G
Query OK, 0 rows affected (0.001 sec)# r, Z5 A2 |- b9 n
# S9 k. w" ~9 L% S& F
MariaDB [loogcn_data]> delete from django_migrations where app='auth';5 V9 y5 Y+ b5 G  n) M
Query OK, 12 rows affected (0.001 sec)# _; T7 R% A9 Y! y2 ?+ |
9 [" J. W. r% Y# I( ~: [
MariaDB [loogcn_data]> delete from django_migrations where app='auth' and app='admin';, [! }) e" }5 ~% ~: M
Query OK, 0 rows affected (0.001 sec)
1 w2 o; ]$ R/ d, [( g6 d( t4 l0 k+ K* O5 l
MariaDB [loogcn_data]> select * from django_migrations;
6 ]5 Z& g# l) Q4 z+----+----------+---------------------------------------+----------------------------+
2 ?9 o$ N; h  U| id | app      | name                                  | applied                    |
, f- X6 v( t3 E+ j6 [+----+----------+---------------------------------------+----------------------------+
/ u& R4 D! p5 o$ l| 16 | admin    | 0001_initial                          | 2026-05-23 02:00:37.817254 |
! s" N4 ]! D2 _2 X8 F* E| 17 | admin    | 0002_logentry_remove_auto_add         | 2026-05-23 02:00:37.827664 |0 o  j+ W# f, @0 P5 W4 G
| 18 | admin    | 0003_logentry_add_action_flag_choices | 2026-05-23 02:00:37.836978 |
' n/ ~  r0 a; C. x8 T$ ^3 _| 19 | sessions | 0001_initial                          | 2026-05-23 02:00:37.991277 |
1 G9 Q3 K+ P. D2 d9 S+----+----------+---------------------------------------+----------------------------+7 D5 O  A! X- n) x
4 rows in set (0.001 sec)
1 o1 \4 V2 e/ p- x: y; z+ g  Q, _( {/ Y! }3 V/ `
MariaDB [loogcn_data]> delete from django_migrations where  app='admin';6 D  M3 |) V" G# @
Query OK, 3 rows affected (0.000 sec)/ r& r$ U8 H  S5 a

% }1 [2 B1 n3 u9 eMariaDB [loogcn_data]> select * from django_migrations;
/ [% w0 Z  n1 Y+ f7 w+----+----------+--------------+----------------------------+
- Q' ~* T) F; W( _9 V# ^| id | app      | name         | applied                    |* U3 X! I6 |' H
+----+----------+--------------+----------------------------+
1 C8 Y0 p5 t# f| 19 | sessions | 0001_initial | 2026-05-23 02:00:37.991277 |
/ ~. T$ m3 _; `/ H' {8 W" c+----+----------+--------------+----------------------------+
" g9 x) k) ~8 c1 row in set (0.000 sec)8 N% C/ R4 A+ g, T( N
2 [7 T, t1 @6 j3 U: t
MariaDB [loogcn_data]> delete from django_migrations where  app='sessions';: o  k- R: P2 ?, n; v$ R' Y
Query OK, 1 row affected (0.001 sec)- F1 d' c# F" H
$ g3 N7 I) O7 ]7 G9 X
MariaDB [loogcn_data]>, @  I2 v/ p  {5 O$ X9 z+ l
清空所有的表,, G3 Y) q- J6 Q$ g. s

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:27:29 | 显示全部楼层
进入 MySQL 终端,删掉当前数据库并重建:sqlDROP DATABASE your_database_name;- u/ i- b- O- i- G
CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
1 f6 B5 v" E$ y) y; b* P3 j请谨慎使用此类代码。重新在终端执行迁移命令:bashpython manage.py migrate
. p- p. v4 T+ C- p0 G请谨慎使用此类代码。如果你使用了方案一或方案二后遇到了其他报错,请告诉我:这是新创建的项目还是维护中的老项目?数据库里是否有需要保留的业务数据?我会为你提供更精准的修复步骤。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2026-5-26 09:33:03 | 显示全部楼层
MariaDB [loogcn_data]> show tables;
$ i& |4 l9 o6 S& o+------------------------+
' m& t9 [4 v5 {* s/ k6 l| Tables_in_loogcn_data  |  B0 R  d8 e1 _3 o% C# B
+------------------------++ r* ^- s, @# w4 ^/ R$ F
| account_user           |
# H: B5 M8 k4 F" C. v( o| auth_group             |
! G: c: e/ L8 j0 ~, y8 o| auth_group_permissions |
( \; T. b( d4 M9 }, k1 x" y| auth_permission        |
( O. _5 l8 l+ B; V| django_admin_log       |
. Q# v& ^; w% I+ W0 }| django_content_type    |
  H; ^" i% r% D: f| django_migrations      |( J/ Z/ }+ L, {6 q4 G6 \5 r& `
| django_session         |
4 b3 J* I  I/ R* d9 r0 T" A+------------------------+) r' D) f" p+ h
8 rows in set (0.001 sec): W$ ]9 [1 N. B. C
9 W* K4 ]8 N4 Y6 G# L; j' z( u
MariaDB [loogcn_data]> drop table if exists auth_group_permissions;
% Y) {6 |7 U7 I- u) m' _" r8 d/ rQuery OK, 0 rows affected (0.058 sec)' ^7 f3 h! v: J) l5 J4 K
5 ]- }0 K1 I1 a
MariaDB [loogcn_data]> drop table if exists django_admin_log;
, b! R( x8 P# I4 k. }Query OK, 0 rows affected (0.056 sec)
% B9 `7 g  M! p/ f9 ~1 t
- ^+ h$ U4 ^3 Z* p% TMariaDB [loogcn_data]> drop table if exists auth_permission ;
$ V# o+ n, M- E7 J$ oQuery OK, 0 rows affected (0.061 sec): ], q8 a6 N# S( J5 K* E
3 `7 p' Z8 C) E7 j2 d" H. h0 m
MariaDB [loogcn_data]> drop table if exists account_user ;: a. G; Q! c6 b* _/ H$ s
Query OK, 0 rows affected (0.060 sec)
. A, l( }" }4 A$ L$ P5 n% A
; l4 O% K3 \, _/ @0 ^! W3 y& Y: _$ oMariaDB [loogcn_data]> drop table if exists  auth_group ;
+ ^# R; [+ N# AQuery OK, 0 rows affected (0.055 sec)
: u; F. H8 x7 S! e2 u
3 O7 q8 b& X& \; FMariaDB [loogcn_data]> drop table if exists  django_session;' l) P& {1 V# D4 P  A- \( j1 p8 [
Query OK, 0 rows affected (0.051 sec)
8 G) ^- B% I% L7 ~8 @: E2 c2 |6 K+ q/ p3 X6 O) f
MariaDB [loogcn_data]> drop table if exists django_content_type;; _6 \) x+ _. G
Query OK, 0 rows affected (0.052 sec)( ~+ P2 l/ n9 V' Z9 o

, f* G! v# P  L+ I. [! Q6 mMariaDB [loogcn_data]> drop table if exists django_migrations ;+ B! y/ e2 z/ h
Query OK, 0 rows affected (0.068 sec)( G9 z$ O( J" o: i% S3 p) N

  `8 h9 M' Q! E$ F' u" C+ vMariaDB [loogcn_data]> show tables;& O* q# \7 h# F
Empty set (0.001 sec)6 C$ Y% T4 [) Y1 o

) R8 U; V# d5 g. N; FMariaDB [loogcn_data]>9 @# d' u. K# }! l3 p9 w1 w  o7 `
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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