Comments (7)
@0xTim Isnt that kinda counter intuitive...... I mean that would indicate I have to query the db before I need to add an entry to see if it exists (since an error is not reported and I cant read to the error to know "it would create a duplicate key") and swap from create to update
like wise if I called update on a row that didnt exist it would not get created.
Use case, a json file of employees with employee numbers provided daily with lots of info for each record that changes, as well as new employees.
Read each json entry as an employee
Get employee number as pk
create or update (in order to determine if I should create or update, I am forced to query to determine the method needed)
I think this is confusing and should be handled more quietly kinda like a hybrid between SQL insert (Fluent create) and and SQL update(Fluent update)
We need a single method that will If Exists Update, else Create ..... maybe an upsert command (update or insert)
Failing Use Case (or at least needs to be changed to determine if exists first):
let object = MyStringModel(id: "EMP12345678K47", name: "John Doe") object.save(on: req).map(to: User.self, {user in print(user.id) < Prints Succesfully ... but nothing is in the db })
Use case needed:
let object = MyStringModel(id: "EMP12345678K47", name: "John Doe") object.upsert(on: req).map(to: User.self, {user in /// update if exists, create if doesnt exist print(user.id) })
from fluent-postgres-driver.
I'm also seeing this issue. I wouldn't expect to be able to save a new model with an arbitrary ID but I would expect it to return an error–not success.
from fluent-postgres-driver.
@ezfe @brntphish you need to use model.create()
instead of model.save()
when supplying the ID. Fluent uses the presence of an ID to determine whether to do a update or create
from fluent-postgres-driver.
@0xTim Ah, then shouldn't model.save()
return an error if you supply an ID that doesn't exist yet?
from fluent-postgres-driver.
@ezfe depends on the DB - normal you want to hand that off to a DB
from fluent-postgres-driver.
UPDATE: (Feel Free to Close)
put here for others that have the same issue.
apparently this now exists as :
object.create(orUpdate: true, on: req)
from fluent-postgres-driver.
My solution to this problem was to define a willCreate
on PostgreSQLModel
that checks the ID field, and to exclusively use create
when making new elements. You could also just set the ID field to nil.
extension PostgreSQLModel {
func willCreate(on connection: PostgreSQLConnection) throws -> EventLoopFuture<Self> {
if self.id != nil {
throw Abort(.forbidden, reason: "Cannot create model with ID")
}
return Future.map(on: connection) { self }
}
}
from fluent-postgres-driver.
Related Issues (20)
- An `id` of `null` throws violates not-null constraint. HOT 1
- Accept URL string in configuration HOT 3
- Saving optional enum (nil) triggers: [ WARNING ] bind $3 type (UNKNOWN 0) does not match expected parameter type (TEXT) HOT 5
- 2.0.0 GM
- Type date but expression is of type text (transformAssignedExpr) HOT 5
- Migrations are now creating int8 fields as char in the DB and then the value gets messed up when querying HOT 1
- Aggregate SUM function fails HOT 1
- Sort by multiple field keys fail
- Postgres error thrown when using advised UUID ID field HOT 1
- [Feature request] Support JSONB column type for nested arrays / PostgreSQL
- simpleQuery flattens columns names to lower case HOT 1
- [Documentation] - We need thorough documentation on README or Wiki HOT 3
- Missing '#include <stdlib.h>'; 'abort' must be declared before it is used HOT 3
- debug logging no longer shows the RAW SQL HOT 4
- let user specify Postgres schema in the connection parameters. HOT 2
- Enum Migration: Adding multiple new cases to an already existing enum does only add the last case HOT 5
- No DEFERRABLE Constraint option
- DatabaseConfigurationFactory build error in 2.7.0 HOT 4
- Select SQL query for the fluent-postgres-driver with bindings
- PSQLError: Opening new connection for pool failed HOT 3
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 fluent-postgres-driver.