Alembic 迁移版本无法识别关系列,只能识别外键

问题描述 投票:0回答:1

我正在尝试部署一个简单的 sqlite model.py 文件。总的来说,Alembic 是有效的,因为它可以识别我在模型中的所有更改。问题是我有两列无法识别,而它们正是关系应该发挥作用的列。

列是:读者,来自“书籍”表和卷,来自“读者”表。

我遵循文档中有关配置 alembic 环境和 models.py 文件语法的内容。

以下是 models.py 代码:

from sqlalchemy import Column, Integer, String, Float, Boolean , ForeignKey
from sqlalch.config.database import Base
from sqlalchemy.orm import relationship 

class Reader(Base):
    __tablename__ = 'reader'
    id = Column(Integer, primary_key= True, index = True)
    name = Column(String)
    subs_date = Column(String)
    volume = relationship("Book", back_populates= "reader")
    
class Book(Base):
    __tablename__ = 'book'
    id = Column(Integer, primary_key=True, index = True)
    name = Column(String)
    year = Column(String)
    reader_id = Column(Integer, ForeignKey("reader.id"))    
    reader = relationship("Reader", back_populates="volume") 
    
class Reservation(Base):
    __tablename__ = 'reserve'
    id = Column(Integer, primary_key= True, index= True)
    amount = Column(Integer)
    date = Column(String)

以下是版本代码:

  """Reserve
    Revision ID: eb00c704340f
    Revises: 
    Create Date: 2023-08-15 22:14:37.304484
    
    """
    from typing import Sequence, Union
    
    from alembic import op
    import sqlalchemy as sa
    
    
    # revision identifiers, used by Alembic.
    revision: str = 'eb00c704340f'
    down_revision: Union[str, None] = None
    branch_labels: Union[str, Sequence[str], None] = None
    depends_on: Union[str, Sequence[str], None] = None
    
    
    def upgrade() -> None:
        # ### commands auto generated by Alembic - please adjust! ###
        op.create_table('reader',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(), nullable=True),
        sa.Column('subs_date', sa.String(), nullable=True),
        sa.PrimaryKeyConstraint('id')
        )
        with op.batch_alter_table('reader', schema=None) as batch_op:
            batch_op.create_index(batch_op.f('ix_reader_id'), ['id'], unique=False)
    
        op.create_table('reserve',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('amount', sa.Integer(), nullable=True),
        sa.Column('date', sa.String(), nullable=True),
        sa.PrimaryKeyConstraint('id')
        )
        with op.batch_alter_table('reserve', schema=None) as batch_op:
            batch_op.create_index(batch_op.f('ix_reserve_id'), ['id'], unique=False)
    
        op.create_table('book',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(), nullable=True),
        sa.Column('year', sa.String(), nullable=True),
        sa.Column('reader_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['reader_id'], ['reader.id'], name='fk_user'),
        sa.PrimaryKeyConstraint('id')
        )
        with op.batch_alter_table('book', schema=None) as batch_op:
            batch_op.create_index(batch_op.f('ix_book_id'), ['id'], unique=False)
    
    def downgrade() -> None:
        # ### commands auto generated by Alembic - please adjust! ###
        with op.batch_alter_table('book', schema=None) as batch_op:
            batch_op.drop_index(batch_op.f('ix_book_id'))
    
        op.drop_table('book')
        with op.batch_alter_table('reserve', schema=None) as batch_op:
            batch_op.drop_index(batch_op.f('ix_reserve_id'))
    
        op.drop_table('reserve')
        with op.batch_alter_table('reader', schema=None) as batch_op:
            batch_op.drop_index(batch_op.f('ix_reader_id'))
    
        op.drop_table('reader')

这是我第一个使用 API 的项目。我已经有了回购协议,如果需要的话我可以分享。期待指导!

编辑:Github 存储库:https://github.com/caioalrodrig/bookies_backend

python database sqlite fastapi alembic
1个回答
1
投票

这是应该的:

relationship
是一个 SQLAlchemy 构造,可以使使用关系数据库更加方便,但它不作为数据库列存在。
reader
字段使用您的
reader_id
外键创建相应的
Reader
对象,并且
volume
字段提供指向读取器的所有
Book
对象的列表。

© www.soinside.com 2019 - 2024. All rights reserved.