How to use the alembic.op.create_foreign_key function in alembic

To help you get started, we’ve selected a few alembic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hypothesis / h / h / migrations / versions / 74bff6a7d9de_make_feature_flag_deletions_cascade.py View on Github external
def upgrade():
    op.drop_constraint(fk_name, "featurecohort_feature", type_="foreignkey")
    op.create_foreign_key(
        op.f(fk_name),
        "featurecohort_feature",
        "feature",
        ["feature_id"],
        ["id"],
        ondelete="cascade",
    )
github ZeusWPI / Haldis / app / migrations / versions / old_migrations / 42709384216_.py View on Github external
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_foreign_key('order_ibfk_1', 'order', 'user', ['courrier_id'], ['id'])
    ### end Alembic commands ###
github cloudify-cosmo / cloudify-manager / resources / rest-service / cloudify / migrations / versions / a6d00b128933_4_4_to_4_5.py View on Github external
op.drop_constraint(
        op.f('groups_tenants_role_id_fkey'),
        'groups_tenants',
        type_='foreignkey')
    op.drop_constraint(
        op.f('groups_tenants_group_id_fkey'),
        'groups_tenants',
        type_='foreignkey')
    op.drop_constraint(
        op.f('groups_tenants_tenant_id_fkey'),
        'groups_tenants',
        type_='foreignkey')
    op.create_foreign_key(u'groups_tenants_role_id_fkey', 'groups_tenants',
                          'roles', ['role_id'], ['id'])
    op.create_foreign_key(u'groups_tenants_tenant_id_fkey', 'groups_tenants',
                          'tenants', ['tenant_id'], ['id'])
    op.create_foreign_key(u'groups_tenants_group_id_fkey', 'groups_tenants',
                          'groups', ['group_id'], ['id'])

    # set null=false
    op.alter_column(
        'groups_tenants',
        'role_id',
        existing_type=sa.INTEGER(),
        nullable=False)

    # dep_up blueprint fks
    op.drop_constraint(
        op.f('deployment_updates__new_blueprint_fk_fkey'),
        'deployment_updates',
        type_='foreignkey')
github ergo / ziggurat_foundations / ziggurat_foundations / migrations / versions / 438c27ec1c9_normalize_constraint_and_key_names.py View on Github external
remote_cols=["resource_id"],
                local_cols=["resource_id"],
                onupdate="CASCADE",
                ondelete="CASCADE",
            )

        if "resources_pkey" == insp.get_pk_constraint("resources")["name"]:
            op.execute("ALTER INDEX resources_pkey RENAME to pk_resources")

        if "resources_owner_group_id_fkey" in [
            c["name"] for c in insp.get_foreign_keys("resources")
        ]:
            op.drop_constraint(
                "resources_owner_group_id_fkey", "resources", type_="foreignkey"
            )
            op.create_foreign_key(
                None,
                "resources",
                "groups",
                remote_cols=["id"],
                local_cols=["owner_group_id"],
                onupdate="CASCADE",
                ondelete="SET NULL",
            )

        if "resources_owner_user_id_fkey" in [
            c["name"] for c in insp.get_foreign_keys("resources")
        ]:
            op.drop_constraint(
                "resources_owner_user_id_fkey", "resources", type_="foreignkey"
            )
            op.create_foreign_key(
github RiotGames / cloud-inquisitor / backend / cloud_inquisitor / data / migrations / versions / e445d703e60f_removing_partitions_adding_foreign_keys.py View on Github external
def upgrade():
    partitions = (
        'tags', 'resources', 'resource_properties', 'issues', 'resource_mappings',
        'issue_properties', 'auditlog', 'logs', 'emails'
    )
    for table in partitions:
        op.execute('ALTER TABLE `{}` REMOVE PARTITIONING'.format(table))

    # ### commands auto generated by Alembic - please adjust! ###
    op.create_foreign_key('fk_resource_mapping_child', 'resource_mappings', 'resources', ['child'], ['resource_id'], ondelete='CASCADE')
    op.create_foreign_key('fk_resource_mapping_parent', 'resource_mappings', 'resources', ['parent'], ['resource_id'], ondelete='CASCADE')
    op.create_foreign_key('fk_resource_property_resource_id', 'resource_properties', 'resources', ['resource_id'], ['resource_id'], ondelete='CASCADE')
    op.create_foreign_key('fk_resource_account_id', 'resources', 'accounts', ['account_id'], ['account_id'], ondelete='CASCADE')
    op.create_foreign_key('fk_resource_types_resource_type_id', 'resources', 'resource_types', ['resource_type_id'], ['resource_type_id'], ondelete='CASCADE')
    op.create_foreign_key('fk_tag_resource_id', 'tags', 'resources', ['resource_id'], ['resource_id'], ondelete='CASCADE')
    # ### end Alembic commands ###
github indico / indico / indico / migrations / versions / 201505061404_3b997c7a4f0c_use_proper_type_and_fk_for_booked_for_id.py View on Github external
def upgrade():
    op.execute('ALTER TABLE roombooking.reservations ALTER COLUMN booked_for_id TYPE int USING booked_for_id::int')
    op.create_foreign_key(None,
                          'reservations', 'users',
                          ['booked_for_id'], ['id'],
                          source_schema='roombooking', referent_schema='users')
    op.create_index(None, 'reservations', ['booked_for_id'], unique=False, schema='roombooking')
github abrt / faf / src / pyfaf / storage / migrations / versions / e17dc14292b9_add_ondelete_cascade_to_report_tables.py View on Github external
['report_id'], ['id'])

    drop_constraint('reportunknownpackages_report_id_fkey', 'reportunknownpackages', type_='foreignkey')
    create_foreign_key('reportunknownpackages_report_id_fkey', 'reportunknownpackages', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportselinuxmodes_report_id_fkey', 'reportselinuxmodes', type_='foreignkey')
    create_foreign_key('reportselinuxmodes_report_id_fkey', 'reportselinuxmodes', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportselinuxcontexts_report_id_fkey', 'reportselinuxcontexts', type_='foreignkey')
    create_foreign_key('reportselinuxcontexts_report_id_fkey', 'reportselinuxcontexts', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportreleasedesktops_report_id_fkey', 'reportreleasedesktops', type_='foreignkey')
    create_foreign_key('reportreleasedesktops_report_id_fkey', 'reportreleasedesktops', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportreasons_report_id_fkey', 'reportreasons', type_='foreignkey')
    create_foreign_key('reportreasons_report_id_fkey', 'reportreasons', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportraw_report_id_fkey', 'reportraw', type_='foreignkey')
    create_foreign_key('reportraw_report_id_fkey', 'reportraw', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportpackages_report_id_fkey', 'reportpackages', type_='foreignkey')
    create_foreign_key('reportpackages_report_id_fkey', 'reportpackages', 'reports',
                       ['report_id'], ['id'])

    drop_constraint('reportopsysreleases_report_id_fkey', 'reportopsysreleases', type_='foreignkey')
    create_foreign_key('reportopsysreleases_report_id_fkey', 'reportopsysreleases', 'reports',
github alephdata / aleph / aleph / migrate / versions / afd5bb9f5004_rename_watchlists.py View on Github external
op.alter_column('permission', 'resource_type', type_=tmp_type,
                    existing_type=old_type)
    meta = sa.MetaData()
    meta.bind = bind
    meta.reflect()
    permission = meta.tables['permission']

    q = sa.update(permission).where(permission.c.resource_type == 'watchlist')
    q = q.values(resource_type='collection')
    op.execute(q)
    old_type.drop(bind, checkfirst=False)
    new_type.create(bind, checkfirst=False)
    op.execute('ALTER TABLE permission ALTER COLUMN resource_type TYPE permission_type'
               ' USING resource_type::text::permission_type;')
    tmp_type.drop(bind, checkfirst=False)
    op.create_foreign_key("collection_creator_id_fkey", "collection", "role",
                          ["creator_id"], ["id"])
    op.create_foreign_key("entity_collection_id_fkey", "entity", "collection",
                          ["collection_id"], ["id"])
github lablup / backend.ai-manager / src / ai / backend / manager / models / alembic / versions / 9a91532c8534_add_scaling_group.py View on Github external
'fifo',
        '{}'::jsonb
    );
    INSERT INTO sgroups_for_domains
    VALUES ('default', 'default');
    '''
    connection = op.get_bind()
    connection.execute(query)

    op.add_column('agents', sa.Column('scaling_group', sa.String(length=64), server_default='default', nullable=False))
    op.create_index(op.f('ix_agents_scaling_group'), 'agents', ['scaling_group'], unique=False)
    op.create_foreign_key(op.f('fk_agents_scaling_group_scaling_groups'),
                          'agents', 'scaling_groups', ['scaling_group'], ['name'])
    op.add_column('kernels', sa.Column('scaling_group', sa.String(length=64), server_default='default', nullable=False))
    op.create_index(op.f('ix_kernels_scaling_group'), 'kernels', ['scaling_group'], unique=False)
    op.create_foreign_key(op.f('fk_kernels_scaling_group_scaling_groups'), 'kernels', 'scaling_groups', ['scaling_group'], ['name'])
    # ### end Alembic commands ###
github 2gis / vmmaster / migrations / alembic / versions / 4ec1ca506d1f_add_foreign_keys.py View on Github external
def upgrade():
    op.create_foreign_key(name="fk_vmmaster_log_step_to_session",
                          source="vmmaster_log_steps",
                          referent="sessions",
                          local_cols=["session_id"],
                          remote_cols=["id"],
                          ondelete='CASCADE')
    op.create_foreign_key(name="fk_session_log_step_to_vmmaster_log_step",
                          source="session_log_steps",
                          referent="vmmaster_log_steps",
                          local_cols=["vmmaster_log_step_id"],
                          remote_cols=["id"],
                          ondelete='CASCADE')