Ver Fonte

feat: 允许comment的father_id为null

SongZihuan há 2 anos atrás
pai
commit
8a03383254
4 ficheiros alterados com 10 adições e 11 exclusões
  1. 1 3
      README.md
  2. 2 2
      app/db.py
  3. 2 1
      main.py
  4. 5 5
      migrations/versions/566a5752c06e_.py

+ 1 - 3
README.md

@@ -4,9 +4,8 @@
 ## 初始化数据库
 ### 使用migration
 ```shell
-$ flask db upgrade e6b4151608e7
+$ flask db upgrade 566a5752c06e
 $ flask shell
-$ >>> from app.db import create_all  
 $ >>> create_all()
 $ >>> quit()
 ```
@@ -14,7 +13,6 @@ $ >>> quit()
 ### 不使用migration
 ```shell
 $ flask shell
-$ >>> from app.db import create_all  
 $ >>> create_all()
 $ >>> quit()
 ```

+ 2 - 2
app/db.py

@@ -132,8 +132,8 @@ class Comment(db.Model):
     content = db.Column(db.Text, nullable=False)
     create_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
     update_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
-    auth_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True, nullable=True)
-    father_id = db.Column(db.Integer, db.ForeignKey("comment.id"), primary_key=True, nullable=True)
+    auth_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
+    father_id = db.Column(db.Integer, db.ForeignKey("comment.id"))
     auth = db.relationship("User", back_populates="comment")
     father = db.relationship("Comment", foreign_keys="[Comment.father_id]", remote_side="[Comment.id]",
                              back_populates="son")

+ 2 - 1
main.py

@@ -19,8 +19,9 @@ app = HTalkFlask(__name__)
 
 @app.shell_context_processor
 def make_shell_context():
-    from app.db import db
+    from app.db import db, create_all
     return {
         "app": app,
         "db": db,
+        "create_all": create_all
     }

+ 5 - 5
migrations/versions/e6b4151608e7_.py → migrations/versions/566a5752c06e_.py

@@ -1,8 +1,8 @@
 """empty message
 
-Revision ID: e6b4151608e7
+Revision ID: 566a5752c06e
 Revises: 
-Create Date: 2022-10-15 18:47:18.044633
+Create Date: 2022-10-16 17:02:52.251961
 
 """
 from alembic import op
@@ -10,7 +10,7 @@ import sqlalchemy as sa
 
 
 # revision identifiers, used by Alembic.
-revision = 'e6b4151608e7'
+revision = '566a5752c06e'
 down_revision = None
 branch_labels = None
 depends_on = None
@@ -47,11 +47,11 @@ def upgrade():
     sa.Column('content', sa.Text(), nullable=False),
     sa.Column('create_time', sa.DateTime(), nullable=False),
     sa.Column('update_time', sa.DateTime(), nullable=False),
-    sa.Column('auth_id', sa.Integer(), nullable=True),
+    sa.Column('auth_id', sa.Integer(), nullable=False),
     sa.Column('father_id', sa.Integer(), nullable=True),
     sa.ForeignKeyConstraint(['auth_id'], ['user.id'], ),
     sa.ForeignKeyConstraint(['father_id'], ['comment.id'], ),
-    sa.PrimaryKeyConstraint('id', 'auth_id', 'father_id')
+    sa.PrimaryKeyConstraint('id')
     )
     op.create_table('follow',
     sa.Column('time', sa.DateTime(), nullable=False),