Maybe I'm missing something here that is super simple, but I have a postgresql db that has several tables structured as such:
- releases
|- releases_contributions
|- releases_primary contributions
|- recording
|- recording_contributions
|- recording_primary_contributions
where releases_contributions
, releases_primary_contributions
& recording
all have a column that has a ForeignKey associated with the auto generated releases_id
.
I've made a revision to update the ondelete
parameter:
op.alter_column('recording', 'releases_id', sa.ForeignKey('releases.releases_id', ondelete='CASCADE'))
op.alter_column('recording_contributions', 'recording_fk_id', sa.ForeignKey('recording.recording_id', ondelete='CASCADE'))
op.alter_column('recording_primary_contributions', 'recording_fk_id', sa.ForeignKey('recording.recording_id', ondelete='CASCADE'))
op.alter_column('releases_contributions', 'releases_fk_id', sa.ForeignKey('releases.releases_id', ondelete='CASCADE'))
op.alter_column('releases_primary_contributions', 'releases_fk_id', sa.ForeignKey('releases.releases_id', ondelete='CASCADE'))
And in my initial alembic upgrade I have this, of course:
op.add_column('releases', 'releases_id', primary_key=True, autoincrement=True)
I'm trying to delete a row in releases
delete from releases where releases_id=1;
that then cascades down to the other tables and deletes any row that has that release as it's foreign key.
But whenever I run that command, I get the following error:
ERROR: update or delete on table "releases" violates foreign key constraint "releases_contributions_releases_fk_id_fkey" on table "releases_contributions"
DETAIL: Key (releases_id)=(1) is still referenced from table "releases_contributions".
What am I missing here?
Updating the alembic revision with the downgrades/upgrades below, then running alembic downgrade -1
and alembic upgrade head
fixed this issue.
def upgrade():
op.alter_column('recording', 'releases_id', sa.ForeignKey('releases.releases_id', ondelete='CASCADE'))
op.alter_column('recording_contributions', 'recording_fk_id', sa.ForeignKey('recording.recording_id', ondelete='CASCADE'))
op.alter_column('recording_primary_contributions', 'recording_fk_id', sa.ForeignKey('recording.recording_id', ondelete='CASCADE'))
op.alter_column('releases_contributions', 'releases_fk_id', sa.ForeignKey('releases.releases_id', ondelete='CASCADE'))
op.alter_column('releases_primary_contributions', 'releases_fk_id', sa.ForeignKey('releases.releases_id', ondelete='CASCADE'))
def downgrade():
op.drop_contraint('releases_id', 'releases')
op.drop_contraint('releases_id', 'recording')
op.drop_contraint('releases_fk_id', 'releases_contributions')
op.drop_contraint('releases_fk_id', 'releases_primary_contributions')
op.drop_contraint('recording_fk_id', 'recording_contributions')
op.drop_contraint('recording_fk_id', 'recording_primary_contributions')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.