Unsupported Scan, storing driver.Value type []uint8 into type *guid.GUID - sql-server

I work with Golang and SQL Server.
My struct in Golang:
type Role struct {
Id guid.GUID `gorm:"primaryKey;column:Id;type:uniqueidentifier" json:"id"`
RoleName string `gorm:"column:RoleName;not null;unique" json:"roleName"`
IsEnable bool `gorm:"column:IsEnable" json:"isEnable"`
Permissions []RolePermission }
I use gorm to query data but receive error:
unsupported Scan, storing driver.Value type []uint8 into type *guid.GUID.
I used uuid before but the id data is wrong when query (guid to uuid).
Is any way to store and work with Guid using Golang and SQL server

Early versions of go-gorm (v0.2) were including UUID/GUID support for SQLTag, with isUUID() a test on the type name ("uuid" or "guid").
But that code is no longer present in current go-gorm v2.0.
You might need to implement a custom Data Type Scanner / Valuer, or use one like google/uuid:
import (
"github.com/google/uuid"
"github.com/lib/pq"
)
type Post struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
Title string
Tags pq.StringArray `gorm:"type:text[]"`
}

Related

Saving and retrieving JSONB columns using SqlKata

I'm manually serializing objects to and from strings and trying to store them in a Postgres database using SqlKata:
var obj = new { Id = 3, JsonB = "{a: 5}" };
dbInstance.Query("TableName").InsertAsync(obj);
The column JsonB is of type jsonb. When I attempt to do it this way, I get the following error:
Exception data:
Severity: ERROR
SqlState: 42804
MessageText: column "DataField" is of type jsonb but expression is of type text
I don't see anywhere in the documentation nor the code to where I can cast types on insert, and am at a loss on how to make it so that I can save JSON strings/objects to the appropriate fields.
I have same problem. For inserts where json column is present in table switched to use NpgsqlCommand and Npgsql.Json.NET library for serializing objects on the fly. Not SqlKata but working:
Product prod;
using (NpgsqlCommand command =
new NpgsqlCommand(#$"INSERT INTO products (json_col) VALUES (#json_col)", connection))
{
command.Parameters.Add(
new NpgsqlParameter("json_col", NpgsqlTypes.NpgsqlDbType.Json) { Value = prod });
await command.ExecuteNonQueryAsync();
}
I don't know SqlKata, but indeed you need to tell Npgsql that you're sending a json (or jsonb) type, since strings are mapped to the PostgreSQL text type by default. PostgreSQL is quite type-strict and will not implicitly cast between most types - you will need to find out how to set NpgsqlDbType via SqlKata.

Embedded structs in aerospike-go library unexpected behaviour

Aerospike Go client
Problem
Need to add set of common fields to all the sets ,ie CreatedAt,UpdtedAt,DeletedAt etc. For the same I have created a struct and embed that with all the set structs. I need the Fields of the common structure saved in the set as fields of the given record
Sample Code
type Table struct {
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
}
type Account struct {
Table
Name string
Status bool
.....
}
For the above mentioned struct Account.I expect the record stored with bin names
CreatedAt,UpdatedAt,DeletedAt,Name,Status.....
But when the records are stored bin names are
Table,Name,Status...
Where Table would be a map with key values
Is it possible to achieve the expected behaviour ? if so how?
Struct embedding embeds methods and doesn't reflect attributes. Attributes of inner type are accessible thorough outer type but don't exist among attributes of outer type. So defining attribute of type Table you literally define attribute of type Table not reflect all attributes of type Table to type Account.
Try to print your Account struct - playground.

Cassandra: map collections with multiple datatypes

As stated and discussed by Mr. Ellis in dynamic-columns/wide-rows, dynamic table is possible through Map Collection. However, I can see that this is only applicable for data with the same types.
Example from the link:
CREATE TABLE users (
user_id text PRIMARY KEY,
name text,
birth_year int,
phone_numbers map
);
INSERT INTO users (user_id, name, birth_year, phone_numbers)
VALUES ('jbellis', 'Jonathan Ellis', 1976, {'home': 1112223333, 'work': 2223334444});
Both home and work phone_numbers are type integer. But we need a collection with various datatypes. Say,
Create table storage ( mobile_id int PRIMARY KEY, date timestamp, data map );
Then data contains these:
{'state': String, 'protocol': Integer, 'weight': Double, 'frame': Blob ... }
So my question is that, do we have an alternative for this? Is this possible with CQL?
At this time, I believe that it is not possible. You would be better off using a String with some sort of type information embedded in it
ie {'home': 'int:1112223333', 'work': 'str:222-333-4444'}
or alternatively use a blob and save a language-specific map into Cassandra using the blob type and language-specific serialization to save your variable map.
Maps are typed. Still you can create one map field per type? map_int, map_text, map_etc
This has the added benefit it'll be a bit faster, as collections are loaded as a whole when read, so splitting up by type will load less data one each query. You should be able to find out the type of what you're looking for up front I hope.

Pentaho salesforce upsert using externalID

I am trying to insert data in salesforce using upsert, for one field i am using the ExternalId field , i have tried many combinations but it fails...I get the error : the syntax should be object:externalId/lookupField
Any idea what is the exact syntax? Keep in mind i am inserting in table Account and the externalId field refers to Account also
object:externalId/lookupField is not very clear is it. There's a comment hidden away in the Pentaho code:
// We use an external key
// the structure should be like this :
// object:externalId/lookupField
// where
// object is the type of the object
// externalId is the name of the field in the object to resolve the value
// lookupField is the name of the field in the current object to update (is the "__r" version)
Lets say you're populating a Salesforce Object Foo__c, which has a Lookup field to Contact called Contact__c. The 'relationship name' for that lookup field would then be Contact__r.
On Contact lets say you have added an External ID called Legacy_Id__c and thats what you want to use when populating Foo__c.
What Pentaho would want in the Module Field column would then be:
Contact:Legacy_Id__c\Contact__r
The bit to the left of the slash is telling Pentaho which object/external id to map to. To the right of the slash, its telling Pentaho which lookup/relationship on Foo__c to fill in.

store strings of arbitrary length in Postgresql

I have a Spring application which uses JPA (Hibernate) initially created with Spring Roo. I need to store Strings with arbitrary length, so for that reason I've annotated the field with #Lob:
public class MyEntity{
#NotNull
#Size(min = 2)
#Lob
private String message;
...
}
The application works ok in localhost but I've deployed it to an external server and it a problem with encoding has appeared. For that reason I'd like to check if the data stored in the PostgreSQL database is ok or not. The application creates/updates the tables automatically. And for that field (message) it has created a column of type:
text NOT NULL
The problem is that after storing data if I browse the table or just do a SELECT of that column I can't see the text but numbers. Those numbers seems to be identifiers to "somewhere" where that information is stored.
Can anyone tell me exactly what are these identifiers and if there is any way of being able to see the stored data in a #Lob columm from a pgAdmin or a select clause?
Is there any better way to store Strings of arbitrary length in JPA?
Thanks.
I would recommend skipping the '#Lob' annotation and use columnDefinition like this:
#Column(columnDefinition="TEXT")
see if that helps viewing the data while browsing the database itself.
Use the #LOB definition, it is correct. The table is storing an OID to the catalogs -> postegreSQL-> tables -> pg_largeobject table.
The binary data is stored here efficiently and JPA will correctly get the data out and store it for you with this as an implementation detail.
Old question, but here is what I found when I encountered this:
http://www.solewing.org/blog/2015/08/hibernate-postgresql-and-lob-string/
Relevant parts below.
#Entity
#Table(name = "note")
#Access(AccessType.FIELD)
class NoteEntity {
#Id
private Long id;
#Lob
#Column(name = "note_text")
private String noteText;
public NoteEntity() { }
public NoteEntity(String noteText) { this.noteText = noteText }
}
The Hibernate PostgreSQL9Dialect stores #Lob String attribute values by explicitly creating a large object instance, and then storing the UID of the object in the column associated with attribute.
Obviously, the text of our notes isn’t really in the column. So where is it? The answer is that Hibernate explicitly created a large object for each note, and stored the UID of the object in the column. If we use some PostgreSQL large object functions, we can retrieve the text itself.
Use this to query:
SELECT id,
convert_from(loread(
lo_open(note_text::int, x'40000'::int), x'40000'::int), 'UTF-8')
AS note_text
FROM note

Resources