易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 568|回复: 1
收起左侧

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

[复制链接]
发表于 2025-7-18 17:17:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
因调试过程中,发现有点问题,就把数据库表从其他客户端删除了,以为删除了就可以同步过去,没想到就是下面的结果。
3 S! O2 ~9 H; s( I' [; |(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py check2 x: X+ ~/ q: E  W
System check identified no issues (0 silenced).
! J0 p6 ]+ g1 o3 Z* u# T6 v8 I) d
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations
, |5 ?& ~/ D3 _- z. l' [$ @+ kNo changes detected
5 W% {- |1 `3 _% t2 O
! e+ z9 ~. Y1 ?, V3 ^3 l(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations order
$ r4 H5 M) l3 u) P# j8 ]# qNo changes detected in app 'order'! @( D5 p% G4 K; k2 I

: B' y2 o4 r! |% l1 c(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order' c' Q; m0 C6 N; ^8 C0 M0 e
Operations to perform:! g' }9 Z' G" [. E( ]2 P
  Apply all migrations: order( V2 k& }' O' V! Y$ c$ ?' d' T8 W# c/ H. y
Running migrations:
/ R; F+ o! ?/ r6 I1 K  No migrations to apply.1 Z$ m; @' r6 w+ H
% ?" D) W% @4 y, k! v3 n+ T
同步不过去,怎么办?# L! D7 G6 W1 ~
看到有高手的同步经历,按照他的方法,去执行了下,好像问题解决了。
, S9 e! B4 p0 t0 R4 K) `- h! ?  Z0 f
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell2 T3 G, @& Z# m# B: y7 X  H
Welcome to the MariaDB monitor.  Commands end with ; or \g.
7 L3 W* t  q8 \5 \/ ~. ~' RYour MariaDB connection id is 152; J* D5 a# Y2 m9 n/ F
Server version: 11.5.1-MariaDB mariadb.org binary distribution% }2 V7 w0 g9 q) ~1 w

" z9 K! x( i/ |0 xCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
" K. e2 O# ~2 R, F/ {
. \/ {9 Q! P) g. J% J* ?/ [Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.7 j- A: |  U4 V) [
* J$ h" A9 v( S+ N
MariaDB [tripsql]> show tables;9 u8 p3 D( J; c+ ^; s% g; ^
+-------------------------------+$ h3 x' r/ s! {' v6 t! D0 b% ~/ I, }
| Tables_in_tripsql             |
, D, D2 d  M8 h; M# a3 K, c0 x, h+-------------------------------+
) l0 F2 o) L" n. j& p| account_user                  |
( ~' v- n& L. [+ k2 v. X! e| account_user_groups           |
2 ]) G6 j6 m% {8 ?) r2 c| account_user_user_permissions |
( q/ V( H4 L9 X. y, H| accounts_login_record         |: f8 k! l5 W0 u
| accounts_user_profile         |1 x' [- G; v2 [; q. W' N$ m4 _
| auth_group                    |- h9 k: K; Z2 H4 P9 q
| auth_group_permissions        |
. S, L7 A$ n9 T2 V| auth_permission               |6 z; _( u4 t/ W8 V0 }! {3 Z- m
| auth_user                     |' i% F2 D% a( l2 `
| auth_user_groups              |
4 b7 R  ~$ k& f9 x: }! T! t  y| auth_user_user_permissions    |  E1 u5 K) n( v, K
| django_admin_log              |
$ [1 u) ~! N: ]3 Z9 v8 p# ~| django_content_type           |9 x# i+ U: c% P7 V, S# }0 s7 H
| django_migrations             |
/ {0 v2 K- `% M( R| django_session                |, s( Y, K# ]0 J: Q. d$ |" q) A) C
| sight                         |
3 N8 Z- B' C! |  R| sight_comment                 |
3 a: ~. ]1 Z0 s! H; \, b( `) N| sight_info                    |
0 {$ P% d% v+ L6 ^( @: G* ^| sight_ticket                  |
# o8 e' x! u; f) z( C| system_image_related          |  w6 O7 P- r2 p) w" ?5 b/ d
| system_slider                 |
- A" N$ v! K( W, `% {+-------------------------------+$ G# R! w: q/ ^9 i" a, w2 X8 {
21 rows in set (0.002 sec)
7 `) R# d% [3 x! f1 m4 n- S+ L& w+ H' U6 L5 m* q* e) Y
( w: B, E& f! x4 n0 d6 M
MariaDB [tripsql]> delete from django_migrations where app='order';; G/ x$ o+ m- c$ @1 V
Query OK, 2 rows affected (0.004 sec)
. A. c- v+ U2 o" F% Y& w$ l; J4 E8 B% Z7 T
MariaDB [tripsql]> exit8 Y/ Z; F5 M+ J, r6 k0 ]
Bye
" q' v: x" N. q; U1 R& n2 g6 K/ C4 u) O3 |6 f
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order
; e) T; o$ T! V+ O* [Operations to perform:$ \+ g* ]  ?5 m2 p
  Apply all migrations: order
" j* p: _0 n+ t# J( mRunning migrations:; ~+ n) t3 B* |6 E
  Applying order.0001_initial... OK
* ]) z* z3 L, a2 ?  q: z! X2 _4 g0 X3 y0 M, I# [
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>" A% d" T( T/ s. I. X/ e
这次成功同步过去了。* t' V4 E3 o9 a6 c; N" j! ]
' k; z( ?# r- E& t) y; Z
6 D2 u( K$ g; _1 A; t& q
8 n1 ~/ O+ |! N) s

! r' I# o0 Q0 N* ~5 l2 m( a9 y4 S1 I, A0 ?

& {/ |: o0 B/ Q$ Z0 d0 D& n% q; b4 E* \1 j; |3 [2 ?
- e5 `3 o9 `5 k1 U* a
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero
4 {5 k! g. m& o7 q9 lOperations to perform:+ v" O) r. Z% I! `$ A
  Unapply all migrations: order" Z+ q7 e- t3 A+ @! W  ~7 M! x1 G
Running migrations:' e2 a! L. m1 g
  Rendering model states... DONE' B7 f6 H; l7 u/ [
  Unapplying order.0001_initial... OK9 S7 c8 Y2 ?, W$ y
3 B+ m6 N/ R/ f
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py migrate order zero0 r# @3 M* O5 v9 [
Operations to perform:8 ?7 }- R& X) J, b1 X
  Unapply all migrations: order% U4 X& c' K" b& @
Running migrations:
: M) F- n# @# j; h2 N$ o- U  No migrations to apply.
8 u/ @6 U9 E$ r3 i% b7 X0 i; H; n# q0 H3 p7 C2 S2 V
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py makemigrations --empty order/ U; {6 y4 i% ~& b0 ~2 U* m
Migrations for 'order':( \$ F5 D- Y. C
  order\migrations\0002_auto_20250718_1752.py3 `: \. o6 o( }& ]/ u
7 ^% Z1 |% b# ]! V% \
(python3.8.2-vitual) C:\Users\admin\Desktop\deskdirectory\trip>python manage.py dbshell# a3 e2 i$ `; Z( Z1 H" A  N; P
Welcome to the MariaDB monitor.  Commands end with ; or \g.6 O. W! k  @. e! C* e  I( W$ C
Your MariaDB connection id is 162) w* ~: }' Y5 U% }
Server version: 11.5.1-MariaDB mariadb.org binary distribution
- p# s# `4 E" t, e% ?
" D* u' H  i9 p4 y4 o$ tCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.' a3 O4 c0 y8 g% c
% d% E3 ~0 K. x. L
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
' B% g8 X# z7 P2 @" Y  m) ?0 k% J% D$ z6 O' ~# @. L/ P
MariaDB [tripsql]> delete from django_migrations where app='order';
% u# |6 ~# }  c: U! MQuery OK, 0 rows affected (0.001 sec)9 Q, M# ?' @" j7 l/ J" L
5 x8 m4 u. @6 c$ e2 t& ?
MariaDB [tripsql]>
% ~* Q2 ^5 u( j
& A  y1 Q' I% Z
- E! ?* l' z  m. s: {. g7 Q
4 G$ S+ O" \% B6 G; f; @- U4 ?6 ~/ \& R1 `8 ]9 r8 F
, j% j5 G8 X7 K( L8 J+ x
7 E1 p7 ]+ P. p# O' U' p
 楼主| 发表于 2025-7-18 17:22:08 | 显示全部楼层
问题描述:
8 J, {, h# E8 p6 Q1 r3 x! d已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrations确认成功,执行python manage.py migrate,提示No migrations to apply. 表示一脸懵逼。再次修改,指定表名,再次尝试,发现问题依旧,表示二脸懵逼
! u' f1 X( C  p1 u, N' j5 h3 |' y" `- T2 f
排查过程
; z0 z% i6 R4 ?1 S4 T9 rpython manage.py dbshell 进到数据库里面,查看是否表已存在
5 Z0 u- P2 |; a" }- D结果:表不存在: ?& G  N( Q9 U. i# {+ d- _( I
检查migrations文件
5 G2 A5 H0 l! q' D- \4 M$ o结果:文件没问题
9 f) t+ o1 j; A; h. }4 N百度 google 各种搜,乱投医,各种尝试
; g  |! y0 M( C解决方案
/ e2 A1 s* F0 U" i8 i' f! @7 Y) b6 t3 P/ spython manage.py dbshell 进到数据库中,执行delete from django_migrations where app='your_appname';
* [+ `: L8 ^* j2 e$ Spython manage.py makemigrations(若migrations文件未删除,可不执行这一步)/ {. P4 B4 a0 ^7 \
python manage.py migrate 好啦,大功告成
5 l/ S# D" y7 v/ b原因分析6 W6 n& h5 s5 w
查看django_migrations表结构
# Z8 j5 i% o: H4 _* w" ~建表语句:
2 w1 |1 M0 [* y2 _: @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); ( w1 D. \0 }) T* K9 ]
原因 ; S  C  c  o+ K5 ~
造成多次应用migrations失败的原因是,当前model是修改过的,原来的migrations已经被我删除,但是,重新生成的migrations使用递增整数记名,所以,在django_migrations表中0001,0002等前面几个数字的文件都已被记录,在Django看来,被记录了就相当于已应用,所以,会出现刚开始的No migrations to apply.- x; D( N5 W( H. P' D# |) N- d7 d
避免方案/ t9 g! ]7 g4 K/ g3 b6 T7 m2 G
有强迫症删除migrations文件的同学(比如我),请同时到数据库中删除相应记录3 ~' g0 I1 O9 s3 T
没有强迫症的同学,可以继续生成新的migrations,旧的就不必理会了+ h' u1 H2 i: {2 w; W: B
题外话5 R) _; s" S5 K0 o
执行python manage.py migrate之后,可以使用python manage.py sqlmigrate appname migrations_num(例如python manage.py sqlmigrate user 0002)查看当前migrations文件对应的sql语句。
" P1 p/ F5 m6 N8 l: Q7 E另外,在使用上述命令查看0002文件的sql语句时发现,django会新建一个表user_new,然后插入user表中的数据,再把user表删掉,再把user_new重命名为user。所以,修改model的时候,不必担心原有数据会丢失。
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 点击这里给我发消息

GMT+8, 2026-3-9 00:30 , Processed in 0.047497 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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