Skip to content

update query from POD type in pgsql  #88

Closed

Description

@Rogni

return `values ~= "` ~ m ~ `=\"" ~ to!string(o.` ~ m ~ `) ~ "\"";`;

how to use update from struct in pgsql ?

struct user {
    size_t id;
    string name;
}

user u;
u.id = 1;
u.name  = "Test";

stmt.update!user(u); generate query UPDATE user SET name="Test" WHERE id=1; and i receive error ERROR: column "Test" does not exist

Activity

  1. SingingBush commented on Feb 11, 2020

    @SingingBush
    Collaborator

    The error is weird as there's no column called Test, I suspect it's simply a misleading error and that the cause of the problem is that user is a reserved word that should be surrounded with ".

    I tested the SQL with https://www.db-fiddle.com/f/hroWDeZav3cgHSkwV8qUFQ/0

    and found that I certainly needed to use double quotes around the tables name:

    CREATE TABLE "user" (
      id INT PRIMARY KEY NOT NULL,
      name TEXT NOT NULL
    );
    
    INSERT INTO "user" (id, name) VALUES (1, 'Initial Value');
    
    UPDATE "user" SET name='Test' WHERE id=1;
    
    SELECT * FROM "user";
  2. Rogni commented on Feb 11, 2020

    @Rogni
    Author

    okey. i create test database and run

    create table custom_table ( 
        id SERIAL PRIMARY KEY, 
        firstname TEXT,
        lastname TEXT
    );
    
    INSERT INTO custom_table(firstname,lastname) VALUES ('TestFirstname','TestLastname');
    

    in main.d

    import ddbc;
    static import config;
    import std.conv: to;
    
    
    private const string CONNECTION_URL = "postgresql://"~ config.HOST 
                                ~ ":" ~ to!string(config.PORT) ~"/"
                                ~ config.DATABASE ~ "?user="
                                ~ config.USER ~",password="
                                ~ config.PASSWORD ~ ",ssl=true";
    
    struct custom_table
    {
        size_t id;
        string firstname;
        string lastname;
    }
    
    
    int main(string args[]) {
        custom_table item;
        item.id = 0;
        item.firstname = "TestFirstname";
        item.lastname = "TestLastname";
        auto connection = createConnection(CONNECTION_URL);
        scope (exit) connection.close();
        auto statement = connection.createStatement();
        scope (exit) statement.close();
        update!custom_table(statement, item);
    
        return 0;
    }
    

    Error:

    2020-02-11T17:23:28.993:pgsqlddbc.d:executeUpdate:670 UPDATE custom_table SET firstname="TestFirstname",lastname="TestLastname" WHERE id=0;
    ddbc.core.SQLException@../../../.dub/packages/ddbc-0.5.1/ddbc/source/ddbc/drivers/pgsqlddbc.d(676): ERROR:  column "TestFirstname" does not exist
    LINE 1: UPDATE custom_table SET firstname="TestFirstname",lastname="...
    
  3. Rogni commented on Feb 12, 2020

    @Rogni
    Author

    why in update query used

    string addUpdateValue(T)(string m) {

    ?
    why not use
    string addFieldValue(T)(string m) {

    ?

  4. self-assigned this
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions