GithubHelp home page GithubHelp logo

Comments (7)

brntphish avatar brntphish commented on May 6, 2024 1

@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.

ezfe avatar ezfe commented on May 6, 2024

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.

0xTim avatar 0xTim commented on May 6, 2024

@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.

ezfe avatar ezfe commented on May 6, 2024

@0xTim Ah, then shouldn't model.save() return an error if you supply an ID that doesn't exist yet?

from fluent-postgres-driver.

0xTim avatar 0xTim commented on May 6, 2024

@ezfe depends on the DB - normal you want to hand that off to a DB

from fluent-postgres-driver.

brntphish avatar brntphish commented on May 6, 2024

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.

ezfe avatar ezfe commented on May 6, 2024

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)

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.