Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Erratic behavior of Update method for datetime field #174

Open
2 tasks done
mahbubabbas opened this issue Nov 1, 2024 · 1 comment
Open
2 tasks done

Bug: Erratic behavior of Update method for datetime field #174

mahbubabbas opened this issue Nov 1, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@mahbubabbas
Copy link

mahbubabbas commented Nov 1, 2024

Describe the bug

I am getting the following error while using Update method even though created_at field of type datetime is not explicitly modified anywhere.

There was a problem with the database: Found changed value for field `created_at`, with record `location:boisar`, but field is readonly

Steps to reproduce

My table is defined as below:

DEFINE TABLE location SCHEMAFULL;
 		
DEFINE FIELD name ON location TYPE string;
DEFINE FIELD address ON location TYPE string;
DEFINE FIELD city ON location TYPE string;
DEFINE FIELD state ON location TYPE string;
DEFINE FIELD country ON location TYPE string;
DEFINE FIELD postal_code ON location TYPE string;

DEFINE FIELD created_at ON location TYPE datetime DEFAULT time::now() READONLY;
DEFINE FIELD updated_at ON location TYPE datetime VALUE time::now();

My struct is below:

type	Location struct {
    ID         *models.RecordID `json:"id,omitempty"`
    Name       string           `json:"name"`
    Address    string           `json:"address"`
    City       string           `json:"city"`
    State      string           `json:"state"`
    Country    string           `json:"country"`
    PostalCode string           `json:"postal_code"`
}

The Update function is called as below:

loc, err := surrealdb.Update[Location](db, *models.ParseRecordID("location:boisar"), surrealdb.Obj{
  "name": "Boisar 2",
  "address": "2nd Main Road",
  "city": "Mumbai",
  "state": "MH",
  "country": "India",
  "postal_code": "400001",
})

I am getting the following error:

There was a problem with the database: Found changed value for field `created_at`, with record `location:boisar`, but field is readonly

Expected behaviour

Update should execute successfully without complaining about created_at field.

SurrealDB version

surreal 2.0.4, surrealdb.go 0.3.0 and 1.0.0-beta, windows 11

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@mahbubabbas
Copy link
Author

mahbubabbas commented Nov 8, 2024

@tobiemh I am using the nightly build of surrealdb and this issue is solved. Thanks for your help. But I have a very serious concern about the go SDK latest version i.e. 0.3. It has lots of short comings compared to the previous version i.e. 0.2. The previous one was much more flexible. Please ponder on the following example:

In the latest one I wrote a func like this which is failing

func (a Attribute) GetAll(sp myconst.SearchParam, whereQry string) (interface{}, error) {
	if whereQry == "" {
		whereQry = "1"
	}

	query := fmt.Sprintf("SELECT *, unit.* FROM %s WHERE %s", myconst.TableAttribute, whereQry)

	search := sp.Search

	if search != "" {
		query += fmt.Sprintf(
			" AND (string::lowercase(name || '') CONTAINS string::lowercase('%s') OR "+
				"string::lowercase(unit.name || '') CONTAINS string::lowercase('%s') OR "+
				"string::lowercase(data_type || '') CONTAINS string::lowercase('%s'))",
			search, search, search,
		)
	}

	if sp.Sort != "" {
		sort := sp.Sort + " ASC"
		if sp.Desc {
			sort = sort + " DESC"
		}
		query += " ORDER BY " + sort
	}

	if sp.Limit > 0 && sp.Start >= 0 {
		query += fmt.Sprintf(" LIMIT %d START %d", sp.Limit, sp.Start)
	}

	res, err := surrealdb.Query[[]AttributeResponse](db.DB, query, nil)
	if err != nil {
		return nil, err
	}

	return PaginatedResponse{
		Result: (*res)[0].Result,
		Total:  GetTotalCount(a.TableName()),
	}, nil
}

Please refer to the issue #180

But in the old SDK (0.2) I could easily write the same with no issue at all

func (a Attribute) GetAll(db *surrealdb.DB, sp myconst.SearchParam, whereQry string) (interface{}, error) {
	if whereQry == "" {
		whereQry = "1"
	}

	query := fmt.Sprintf("SELECT *, unit.* FROM %s WHERE %s", myconst.TableAttribute, whereQry)

	search := sp.Search

	if search != "" {
		query += fmt.Sprintf(
			" AND (string::lowercase(name || '') CONTAINS string::lowercase('%s') OR "+
				"string::lowercase(unit.name || '') CONTAINS string::lowercase('%s') OR "+
				"string::lowercase(data_type || '') CONTAINS string::lowercase('%s'))",
			search, search, search,
		)
	}

	if sp.Sort != "" {
		sort := sp.Sort + " ASC"
		if sp.Desc {
			sort = sort + " DESC"
		}
		query += " ORDER BY " + sort
	}

	if sp.Limit > 0 && sp.Start >= 0 {
		query += fmt.Sprintf(" LIMIT %d START %d", sp.Limit, sp.Start)
	}

	res, err := SmartUnmarshal(db.Query(query, nil))
	if err != nil {
		return nil, err
	}

	return &PaginatedResponse{
		Result: res,
		Total:  GetTotalCount(db, a.TableName()),
	}, nil
}

The the latest version is not allowing just to return interface{}. Also another serious concern is mentioned in the bug above CBOR is hindering the performance of all methods unnecessarily. I really also don't understand and like the introduction of models.RecordID and models.CustomeDateTime fields as all structs are expecting record id of that type which is bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants