依赖性规则尝试清除主键列

时间:2019-02-04 13:17:44

标签: python sqlalchemy alembic

我正在使用alembic创建一些表:

def upgrade():

    op.create_table(
        'dataset',
        sa.Column('id', sa.VARCHAR(64), primary_key=True),
        sa.Column('language', sa.String(8), nullable=False),
        sa.Column('name', sa.String(64), nullable=False)
    )

    op.create_table(
        'speaker',
        sa.Column('id', sa.VARCHAR(64), primary_key=True),
        sa.Column('dataset_id', sa.VARCHAR(64), nullable=False),
        sa.ForeignKeyConstraint(('dataset_id',), ['dataset.id'], ondelete='cascade')
    )

    op.create_table(
        'text_source',
        sa.Column('id', sa.VARCHAR(64), primary_key=True),
        sa.Column('dataset_id', sa.VARCHAR(64), nullable=False),
        sa.ForeignKeyConstraint(('dataset_id',), ['dataset.id'], ondelete='cascade')
    )

    op.create_table(
        'audio_sample',
        sa.Column('id', sa.VARCHAR(64), primary_key=True),
        sa.Column('speaker_id', sa.VARCHAR(64), primary_key=True),
        sa.Column('text_source_id', sa.VARCHAR(64), primary_key=True),
        sa.ForeignKeyConstraint(('speaker_id',), ['speaker.id'], ondelete='cascade'),
        sa.ForeignKeyConstraint(('text_source_id',), ['text_source.id'], ondelete='cascade')
    )

并且我创建了一些对应的实体类:

class DatasetEntity(Base):
    __tablename__ = 'dataset'    
    id = Column(VARCHAR, primary_key=True)

    speakers = relationship('SpeakerEntity')
    text_sources = relationship('TextSourceEntity')


class TextSourceEntity(Base):
    __tablename__ = 'text_source'    
    id = Column(VARCHAR, primary_key=True)
    dataset_id = Column(String, ForeignKey('dataset.id'))

    dataset = relationship('DatasetEntity', back_populates='text_sources')
    audio_samples = relationship('AudioSampleEntity')


class SpeakerEntity(Base):
    __tablename__ = 'speaker'    
    id = Column(String, primary_key=True)
    dataset_id = Column(String, ForeignKey('dataset.id'))

    dataset = relationship('DatasetEntity', back_populates='speakers')
    audio_samples = relationship('AudioSampleEntity')


class AudioSampleEntity(Base):
    __tablename__ = 'audio_sample'    
    id = Column(String, primary_key=True)
    text_source_id = Column(String, ForeignKey('text_source.id'), primary_key=True)
    speaker_id = Column(String, ForeignKey('speaker.id'), primary_key=True)

    speaker = relationship('SpeakerEntity', back_populates='audio_samples')
    text_source = relationship('TextSourceEntity', back_populates='audio_samples')

当我尝试运行用于插入一些数据的脚本时,我得到了:

...
  File "/home/sfalk/miniconda3/envs/t2t/lib/python3.5/site-packages/sqlalchemy/orm/dependency.py", line 571, in process_saves
    state, child, None, True, uowcommit, False
  File "/home/sfalk/miniconda3/envs/t2t/lib/python3.5/site-packages/sqlalchemy/orm/dependency.py", line 591, in _synchronize
    sync.clear(dest, self.mapper, self.prop.synchronize_pairs)
  File "/home/sfalk/miniconda3/envs/t2t/lib/python3.5/site-packages/sqlalchemy/orm/sync.py", line 87, in clear
    "column '%s' on instance '%s'" % (r, orm_util.state_str(dest))
AssertionError: Dependency rule tried to blank-out primary key column 'audio_sample.speaker_id' on instance '<AudioSampleEntity at 0x7f92307ff160>'

,我不太理解这意味着什么或如何理解它。

audio_sample.speaker_id消隐“主键列”是什么意思?

0 个答案:

没有答案