Comments (1)
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:
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)
- Raw query returning a bigint for an int field HOT 1
- error Command "up" not found. HOT 1
- Repeated alter migrations in Postgres for `String[] @default([]) @db.VarChar(64)`
- In "prisma" mode, executing prisma db pull results in the loss of generated fields.
- Dynamic load relations
- Error Thrown When Sorting Field Is Not Selected HOT 3
- ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "42P05", message: "prepared statement \"s0\" already exists", severity: "ERROR", detail: None, column: None, hint: None }), transient: false })]
- How to Perform Group By with Date Extraction in Prisma
- Unable to require libquery_engine-debian-openssl-3.0.x.so.node HOT 2
- Hello @bso-oo 👋 HOT 2
- Run Prisma on docker use elysia/Bun js HOT 1
- Inconsistent column data: Malformed ObjectID: provided hex string representation must be exactly 12 bytes, instead got HOT 2
- Support for manually managing prepared statements HOT 5
- Migration failed to apply cleanly to shadow database HOT 3
- VSCode Autocomplete Not Functioning When Extending Prisma Schema HOT 2
- Prisma Migrate Drops Custom Migrations HOT 5
- Types broken on void clients (no models) HOT 2
- RustPanic: RuntimeError HOT 1
- internal error: entered unreachable code HOT 1
- I'm using using prisma in auth.config.ts and Its giving vercel edge errors
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prisma.