Reputation: 12275
I'm loading some data from a CSV file that has some extra notes fields that I don't want in the DB. Is there an option to just ignore extra fields when storing to the DB?
I think mongoose did this by default - which does have a downside that stuff goes missing without warning if your schema is wrong but... thats what i want in this case.
Otherwise what is a way to reflect and get the schema so I can remove extra fields from the data manually?
I'm getting this error on .create
Unknown arg `notes` in data.notes for type WalletCreateInput.
Did you mean `name`?
Available args:
...
Upvotes: 4
Views: 4389
Reputation: 483
I created a custom generator "prisma-generator-omit-extra-fields" to handle this problem.
It's of course not a perfect solution, but at least something.
E.g. for user model it will generate next utility functions:
polishUser(user); // remove extra fields but has no explicit types
polishDefaultUser(user); // remove extra fields and is suitable for "data" field in queries
polishPartialUser(user); // remove extra fields and is suitalble for "where" field in queries
The more detailed explanation can be found here.
Upvotes: 0
Reputation: 652
Late to the party, but there is a way around this. If you use the "fieldReference" preview feature:
generator client {
provider = "prisma-client-js"
previewFeatures = ["fieldReference"]
}
You can then create the following to strip out any extra keys.
function stripPrisma<T extends {}>(input: {fields:{}},data: T) : T {
let validKeys = Object.keys(input.fields);
let dataCopy: any = {...data};
for(let key of Object.keys(data)) {
if(!(validKeys.includes(key))) {
delete dataCopy[key];
}
}
return dataCopy as T;
}
And use it like this
data = stripPrisma(prisma.myTable, data);
prisma.myTable.create({data:data});
It is not perfect, since it will only be able to use "checked input", meaning you can only use the foreign key in your input and not the foreign object.
Upvotes: 1
Reputation: 7558
It's not allowed to add extra fields while interacting with Prisma Query.
The current behaviour is intentional and it throws the error as an extra validation layer to make sure you're passing the right data.
There is a feature request that discusses allowing extra fields while interacting with Query.
As of now, destructuring the fields which are necessary and passing only the required fields is the only option.
Upvotes: 2