Code Monkey home page Code Monkey logo

Comments (1)

jpaylor avatar jpaylor commented on May 25, 2024

I have run into the exact same issue as well, so I did some digging to see if I could see what might be causing it. I think the issues lies within Prisma Schema Engine however as I don't know Rust I don't feel I can contribute a solution and I'm hoping someone else who does will be able to pick this up.

This is what I found:

https://github.com/prisma/prisma-engines/blob/e66d30dc00faf0c9d53c02f47a511bb304054f91/schema-engine/connectors/sql-schema-connector/src/sql_schema_differ/table.rs#L69-L75

pub(crate) fn foreign_key_pairs(&self) -> impl Iterator<Item = MigrationPair<ForeignKeyWalker<'schema>>> + '_ {
        self.previous_foreign_keys().filter_map(move |previous_fk| {
            self.next_foreign_keys()
                .find(move |next_fk| foreign_keys_match(MigrationPair::new(&previous_fk, next_fk), self.db))
                .map(move |next_fk| MigrationPair::new(previous_fk, next_fk))
        })
    }

The above function seems to compare foreign keys in the database with those in the schema (using the shadow db?), matching them together based on the criteria specified in the foreign_keys_match function...

    references_same_table
        && references_same_column_count
        && constrains_same_column_count
        && constrains_same_columns
        && references_same_columns
        && same_on_delete_action
        && same_on_update_action

The foreign_keys_match function does not look at whether the constraint_name matches, it only looks at other attributes such as whether the same table or columns are referenced. In the example @jcoveney-anchorzero provided this could mean that the foreign key prisma_bug_1_a can be matched to prisma_bug_1_b as they are identical except for by constraint name. This is somewhat understandable because it would need to catch when a foreign key had been renamed to then carry out that rename change in a migration, which is what happens in the function push_foreign_key_pair_changes on lines 496-525.

    if fk
        .map(|fk| fk.constraint_name())
        .transpose()
        .map(|names| names.previous != names.next)
        .unwrap_or(false)
    {
        // Rename the foreign key.


        // Since we are using the conventional foreign key names for the foreign keys of
        // many-to-many relation tables, but we used not to (we did not provide a constraint
        // names), and we do not want to cause new migrations on upgrade, we ignore the foreign
        // keys of implicit many-to-many relation tables for renamings.
        if fk.map(is_prisma_implicit_m2m_fk).into_tuple() == (true, true) {
            return;
        }


        if db.flavour.can_rename_foreign_key() {
            steps.push(SqlMigrationStep::RenameForeignKey {
                foreign_key_id: fk.map(|fk| fk.id),
            })
        } else {
            steps.push(SqlMigrationStep::AddForeignKey {
                foreign_key_id: fk.next.id,
            });
            steps.push(SqlMigrationStep::DropForeignKey {
                foreign_key_id: fk.previous.id,
            })
        }
    }
}

What we are missing is some sort of check in the foreign_key_pairs function to see whether or not we have multiple foreign keys that match each other except for by name.

Interestingly, this situation seems to have already been handled where indexes are concerned in the index_pairs function:

    pub(crate) fn index_pairs<'a>(&'a self) -> impl Iterator<Item = MigrationPair<IndexWalker<'schema>>> + 'a {
        let singular_indexes = self.previous_indexes().filter(move |left| {
            // Renaming an index in a situation where we have multiple indexes
            // with the same columns, but a different name, is highly unstable.
            // We do not rename them for now.
            let number_of_identical_indexes = self
                .previous_indexes()
                .filter(|right| {
                    left.column_names().len() == right.column_names().len()
                        && left.column_names().zip(right.column_names()).all(|(a, b)| a == b)
                        && left.index_type() == right.index_type()
                })
                .count();


            number_of_identical_indexes == 1
        });


        singular_indexes.filter_map(move |previous_index| {
            self.next_indexes()
                .find(|next_index| indexes_match(previous_index, *next_index, self.db.flavour))
                .map(|renamed_index| MigrationPair::new(previous_index, renamed_index))
        })
    }

Maybe we just need something like this fix but for foreign keys i.e. filter the foreign keys down to a list of singular_fks before pairing them together for comparison?

from prisma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.