surrealdb / surrealdb.go

SurrealDB SDK for Golang
https://surrealdb.com
Apache License 2.0
232 stars 60 forks source link

Documentation: working with record links #103

Closed Nickersoft closed 10 months ago

Nickersoft commented 10 months ago

Description

I'm not sure if this is an issue shared across all SurrealDB SDKs right now due to how the underlying RPC works, but I'm currently trying to create new records using db.Create and passing an array of model IDs as strings inside my interface. However, it doesn't appear that these relations are getting saved in the database, despite the other fields in my interface being stored. My table is schemafull and configured to have the specific column be of type set<record>.

Is working with relations not currently supported, or is just currently missing from the docs? If you should be able to create relations via string arrays, happy to open a bug report instead.

Is there an existing issue for this?

Code of Conduct

ElecTwix commented 10 months ago

If you are trying to create an array of records Golang SDK does not support bulk creation such as array but it is planned to have.

If thats is not the case: Could you give me an example to reproduce the issue?

Nickersoft commented 10 months ago

Hey @ElecTwix! Thanks for the quick response – to clarify, I'm not trying to create records in bulk, just store an array of record links as a column on one of my tables. My example repo is a bit complex ATM, and I'd need to carve out time to create an MVP.

That said, the general steps I'd follow to reproduce are:

  1. Using either SurrealQL or a GUI like Surrealist (what I used), create a schemafull table (e.g. "table1") that has a column of type set<record>
  2. Create another table (e.g. "table2") that stores some data for the new column to reference
  3. Using the SDK, attempt to create a new table row that references the test table:
db.Create("table1", map[string]interface{}{
  "links": []string{"table2:1234"}
}

This is effectively the approach I'm trying, except the query passes but links would still be [] in the database.

ElecTwix commented 10 months ago

Hi @Nickersoft!

I created basic program for doing something similar I know is very dirty but it should work Also I wanted to note this is schemaless but I think it will work schemafull when change the user creation part

package main

import (
    "fmt"

    _ "github.com/rs/zerolog"
    "github.com/surrealdb/surrealdb.go"
    "github.com/surrealdb/surrealdb.go/pkg/gorilla"
)

type User struct {
    Name    string
    Surname string
    Friends []string
}

func main() {
    // Connect to SurrealDB
    ws, err := gorilla.Create().Connect("ws://localhost:8000/rpc")
    if err != nil {
        panic(err)
    }
    db, err := surrealdb.New("ws://localhost:8000/rpc", ws)
    if err != nil {
        panic(err)
    }

    // Sign in
    if _, err = db.Signin(map[string]string{
        "user": "root",
        "pass": "root",
    }); err != nil {
        panic(err)
    }

    // Select namespace and database
    if _, err = db.Use("test", "test"); err != nil {
        panic(err)
    }

    var recordArr []string

    for i := 0; i < 10; i++ {
        user := User{
            Name: fmt.Sprintf("John %d", i),
        }
        _, err := db.Create(fmt.Sprintf("user:%d", i), user)
        if err != nil {
            panic(err)
        }
        recordArr = append(recordArr, fmt.Sprintf("user:%d", i))
    }

    // Create user struct
    user := User{
        Name:    "John",
        Surname: "Doe",
        Friends: recordArr,
    }

    // Insert user
    data, err := db.Create("user", user)
    if err != nil {
        panic(err)
    }

    fmt.Println(data)

}
Output:
[map[Friends:[user:0 user:1 user:2 user:3 user:4 user:5 user:6 user:7 user:8 user:9] Name:John Surname:Doe id:user:7b6z7d17dex59f4rd7yq]]
Nickersoft commented 10 months ago

Hey @ElecTwix,

Sorry for the delay – just got around to looking at this. I did a bit more digging and it looks like it's a bug with SurrealDB and schemafull tables. If I make my table schemaless it works as expected, despite the record links being stored as an array of strings. I can use the dot notation to reference nested fields. Going to close this, as I'm tracking it on the main repo now. Thanks for all the help!