Gorm + MSSQL = not seeing the association - sql-server

I have two models that are linked via UUID, but for some reason Gorm doesn't see them,
type PERSON struct {
PERS_ID mssql.UniqueIdentifier `gorm:"primary_key" json:"id,omitempty"`
ORG_ID mssql.UniqueIdentifier `json:"orgId,omitempty"`
LAST_NAME mssql.VarChar `json:"lastName,omitempty"`
FIRST_NAME mssql.VarChar `json:"firstName,omitempty"`
MIDDLE_NAME mssql.VarChar `json:"middleName,omitempty"`
TAB_NUM mssql.VarChar `json:"tabNum,omitempty"`
SCH_ID mssql.UniqueIdentifier `json:"schId,omitempty"`
PRIMARY_IDENTIFIER mssql.UniqueIdentifier `json:"primaryIdentifier,omitempty"`
TYPE_ID mssql.UniqueIdentifier `json:"typeId,omitempty"`
ORG Organization `gorm:"foreignKey:ORG_ID;references:ORG_ID" json:"organization"`
}
type Organization struct {
ORG_ID mssql.UniqueIdentifier `gorm:"primary_key" json:"id,omitempty"`
ORG_NAME mssql.VarChar `json:"name,omitempty" `
ORG_DESC mssql.VarChar `json:"desc,omitempty"`
PARENT_ID mssql.UniqueIdentifier `json:"ParentId"`
LEVEL int `json:"level"`
SCH_ID mssql.UniqueIdentifier `json:"schId"`
}
when you make a request you get null instead of a relation
I tried a different style of writing fields (which did not work), but if you manually drive in, then the search goes
var organizations = models.Organization{ORG_ID: person.ORG_ID}
models.DbParsec3.First(&organizations)

Related

Can't create Portal User. REQUIRED_FIELD_MISSING, Required fields are missing: [Profile]: [Profile]

I am trying to create Portal User from Community public page using this code:
this.userVar = new User(
Alias = (this.contactVar.FirstName + this.contactVar.LastName).toLowerCase(),
Email = this.contactVar.Email,
FirstName = this.contactVar.FirstName,
LastName = this.contactVar.LastName,
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_AU',
Country = 'Australia',
ProfileId = assistanceHubProfile.Id,
IsActive = true,
TimeZoneSidKey = 'Australia/Sydney',
Username = this.contactVar.Email,
ContactId = this.contactVar.Id
);
System.debug(this.userVar);
insert this.userVar;
but I have this error:
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Profile]: [Profile]
System debug before insert:
User:{Alias=tedsttestte, Email=test#test.com, FirstName=Tedst, LastName=Testte, EmailEncodingKey=UTF-8, LanguageLocaleKey=en_US, LocaleSidKey=en_AU, Country=Australia, ProfileId=00e920000002Ag1AAE, IsActive=true, TimeZoneSidKey=Australia/Sydney, Username=test#test.com, ContactId=00392000003RLeXAAW}
As you can see ProfileId is populated. I can't understand why this error is occurred.
There are no triggers and triggered-flow for User SObject
It was because I didn't use without sharing key words in apex class definition. In my case apex class will use with sharing settings by default.

Golang append to a slice of type

I am doing an ldap query, and I want to populate the result into a slice. The result looks something like
objectClass [top person organizationalPerson user]
cn [user.1]
sn [one]
description [user.1]
givenName [user]
distinguishedName [CN=user.1,OU=random,DC=example,DC=com]
...
I am trying to populate it to a map and for that, I created a type.
type keyvalue map[string]interface{}
Now I want to create a slice of this type, so that the data would look something like this for multiple users taken
objectClass [top person organizationalPerson user]
cn [user.1]
sn [one]
description [user.1]
givenName [user]
distinguishedName [CN=user.1,OU=random,DC=example,DC=com]
...
objectClass [top person organizationalPerson user]
cn [user.2]
sn [one]
description [user.2]
givenName [user]
distinguishedName [CN=user.2,OU=random,DC=example,DC=com]
...
For that, I created a slice of the type that I created above.
userslice := make([]keyvalue, 1, 1)
How will I append each users's parameters into the slice in each iteration?
Just use keyvalue instead of map[string]interface{} in your code:
type keyvalue map[string]interface{}
....
user1 := make(keyvalue)
user1["distinguishedName"] = "[CN=user.1,OU=random,DC=example,DC=com]"
user1["givenName"] = "user"
var userslice []keyvalue
userslice = append(userslice, user1)
fmt.Printf("%#v", userslice)

SQLAlchemy Declarative - schemas in SQL Server and foreign/primary keys

I'm struggling to create tables that belong to a schema in a SQL Server database, and ensuring that primary/foreign keys work correctly.
I'm looking for some examples of code to illustrate how this is done
The ingredients needed for this are __table_args__ and the use of the schema prefix on the ForeignKey
DBSession = sessionmaker(bind=engine)
session = DBSession()
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship
Base = declarative_base()
class Table1(Base):
__tablename__ = 'table1'
__table_args__ = {"schema": 'my_schema'}
id = Column(Integer,primary_key = True)
col1 = Column(String(150))
col2 = Column(String(100))
reviews = relationship("Table2", cascade = "delete")
class Table2(Base):
__tablename__ = 'table2'
__table_args__ = {"schema": 'my_schema'}
id = Column(Integer,primary_key = True)
key = Column(Integer)
col2 = Column(String(100))
key = Column(Integer, ForeignKey("my_schema.table1.id"), index=True)
premise = relationship("Table1")
Base.metadata.create_all(bind=engine)

Need help for a query

my current query is:
public function teachers_manage() {
$this->db->select('users.user_id, teacher_id, COUNT(student_id) AS S, COUNT(DISTINCT(users.class)) AS C, schools.region, schools.school_name');
$this->db->from('teacher_student_conn');
$this->db->join('users', 'teacher_student_conn.student_id=users.user_id','left');
$this->db->join('schools', 'schools.school_id=users.school_id');
$this->db->where('users.deactivated_at = "0000-00-00 00:00:00" OR users.deactivated_at IS NULL ');
$this->db->where('users.role_id', '1');
$this->db->group_by("teacher_student_conn.teacher_id");
$result=$this->db->get();
return $result->result();
}
It shows me teachers and for each teacher number of classes he teaches and number of students he teaches. I have made join 2 tables - users and teacher_student_conn by users=user_id=teacher_student_conn.student_id and I've put where clause for student not to be deactivated - shows active students. But how to do that also fot the teachers? Another join will change the results. I only want to add where clause for teachers to be active.
My tables look like this:
users
user_id
school_id
class
role_id
created_at
deactivated_at
teacher-student_conn
id
teacher_id
student_id
created_at
I suggest to add a column for "status" to more easily distinguish the status of student and teacher.
And this is my recommended query
SQL Query:
select
from users a
join teacher-student_conn b on a.user_id = b.student_id
join schools c on c.school_id = a.school_id
where (a.status = "Deactivated") and (b.status = "Deactivated")
For codeigniter:
$this->db->select('a.user_id, b.teacher_id, COUNT(b.student_id) AS S, COUNT(DISTINCT(a.class)) AS C, c.region, c.school_name');
$this->db->join('teacher_student_conn b', 'b.student_id = a.user_id','left');
$this->db->join('schools c', 'c.school_id = a.school_id');
$this->db->where('a.status = "Deactivated" or b.status = "Deactivated" ');
$this->db->where('a.role_id', '1');
$this->db->group_by("b.teacher_id");
$result=$this->db->get("users a");
return $result->result();

power query excel table mssql returns invalid column name

Here is my Customer Parameters table
let
Source = Excel.CurrentWorkbook(){[Name="Customer"]}[Content],
Customer1 = Source{0}[Customer]
in
Customer1
And my Query
let
Customer = Excel.CurrentWorkbook(){[Name="Customer"]}[Content],
Customer_Name = Customer{0}[Customer],
Source = Sql.Database("SERVER", "DATABASE", [Query="SELECT i.PriorityType as 'PRIORITY','COUNT' = COUNT(i.IncidentID)#(lf)FROM ININ_ISupport_S_Incident_Search2 (NULL) i#(lf)WHERE IncidentType IN ('Customer Support','Managed Services')#(lf)AND Organization = Customer_Name#(lf)AND IsResolved = 0#(lf)AND Active = 1#(lf)AND StateType = 'Open'#(lf)GROUP BY i.PriorityType#(lf)ORDER BY CASE#(lf) WHEN i.PriorityType = 'Code Red' THEN 1#(lf) WHEN i.PriorityType = 'Code Red RCA' THEN 2#(lf) WHEN i.PriorityType = 'High' THEN 3#(lf) WHEN i.PriorityType = 'Medium' THEN 4#(lf) WHEN i.PriorityType = 'Low' THEN 5#(lf)END ASC"])
in
Source
I am setting Organization = Customer_Name but I keep getting the following
Message=Invalid column name 'Customer_Name'
Is there a way to accomplish this
Customer_Name is not a column in your SQL table, so it fails with that message. If you want to use Customer_Name in the query, it needs to be added to the string. This is done using the & operator. In your case, you would do something like
"...AND Organization = '" & Customer_Name & "'#(lf)AND IsResolved...
That will cause problems if the customer name has a quote in it (like O'Neill), so you could do
"...AND Organization = '" & Text.Replace(Customer_Name, "'", "''") & "'#(lf)AND IsResolved...
to try and escape quotes.
Escaping quotes is not always sufficient, so if you're running this query on a database where outsiders can create customers you should avoid using the above method. If you filter the column by Customer_Name (through Table.SelectRows(tableName, each [Organization] = Customer_Name)), you should get the same result without any security concerns.

Resources