I am using GORM in my Go Gin API and want to create an association between my User and the Role table. So far, I have simply followed the documentation, but I am running into a problem.
Here are my used models:
type User struct {
gorm.Model
FirstName string
LastName string
Email string `gorm:"unique"`
Password []byte
Roles []Role `gorm:"many2many:user_roles"`
}
type Role struct {
gorm.Model
Name string
}
While I'm still developing, I want to write data directly to the database when I start the API. For this I create the following:
Db.Create(&User{
FirstName:"Jon",
LastName:"Doe",
Email: "jon#doe.local",
Password: encoded,
Roles: []Role{
{Name: "admin"},
{Name: "service"},
},
})
Db.Create(&User{
FirstName:"Jon",
LastName:"Doe",
Email: "jon2#doe.local",
Password: encoded,
Roles: []Role{
{Name: "admin"},
{Name: "service"},
},})
This way I want each name to appear only once in the role table. The corresponding ID should be used in the automatically created user_roles table each time such an entry is created.
What happens, however, is this:
Roles Table:
id
name
1
admin
2
service
3
admin
4
service
User_Role Table:
user_id
role_id
1
1
1
2
2
3
2
4
I have read through https://gorm.io/docs/associations.html and tried Omit but nothing worked yet.
I hope someone has an idea or already solved the same problem. Thanks a lot!
Edit:
When trying "try sending just the role IDs instead of role names" i do it like this:
Db.Create(&User{
FirstName:"Jon",
LastName:"Doe",
Email: "jon2#doe.local",
Password: encoded,
Roles: []Role{
{ID: 1},
{ID: 2},
},})
This gives me the error
cannot use promoted field Model.ID in struct literal of type Role
2. Edit
The ID needs to be listed seperated in the Role model, it does not work just with the gorm.Model
type Role struct {
gorm.Model
ID uint `gorm:"primarykey"`
Name string
}
Related
I am using this article as an example
https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-model-partition-example with a Users container with userId and username and the partition key as userId.
{
"id": "54c7da13-f4b8-4668-90dc-7c1aa968a73e",
"userId": "54c7da13-f4b8-4668-90dc-7c1aa968a73e",
"type": "user",
"username": "jeffw"
}
In my create user page I want to make sure the username is unique before adding a new user. I tried a pre-trigger but found that "You can't run stored procedures or triggers across multiple logical partitions." How do I make sure that when a user is created that they have selected a unique username? I think I could change the partition key to username but why does the article use userId instead?
SOLUTION
See answer from #mark-brown.
Create a unique key on the Users container and /username:
await database.Database.DefineContainer(name: "Users", partitionKeyPath: "/userId")
.WithUniqueKey().Path("/username").Attach()
.CreateIfNotExistsAsync();
Then try to create a new User with userId as "unique_username" and the new username that is attempting to be created:
{
"id": "06af2937-4677-4d27-a167-5517aa6d0ffd",
"userId": "unique_username",
"type": "unique_username",
"username": "jeffw"
}
await _usersContainer.CreateItemAsync(uniqueUser, new PartitionKey("unique_username"));
This will return a Conflict status if the username already exists. Example is here https://github.com/jwidmer/AzureCosmosDbBlogExample/blob/master/BlogWebApp/Services/BlogCosmosDbService.cs
Changing the partition key to username won't help because you can have multiples of that value in your container. One way you could do this is to have a new partition where you store a unique instance for every user name and use a unique index on the container (unique indexes are unique within a logical partition).
Create a new type = "unique_user" and a userId = "unique_user". Then add a new record of that type with the new username as they register. This should get you millions of users without going over the 20GB limit. Then when creating a new user do an insert on the container with the "unique_user" type and id with the new username. If you get a 201 then do another insert with type= "user" and the rest of the user data.
hope that helps.
You can set up an index policy for unique values.
I've got the following (test) collections created in Realm:
RoundType (collection)
uuid (string)
name (string)
data in RoundType (1 row)
uuid: 234dfg345dfg345 name: Fred Flintstone
TestMutate (collection)
uuid (string)
roundType (RoundType)
data in TestMutate: nothing, yet
I want to CREATE a new row in TestMutate, where the "roundType" field in TestMutate is a reference to the existing RoundType row.
I tried the following in the hope that GraphQL knew the roundType existed but obviously that's not right.
mutation {
result: addTestMutate(input: {uuid: "abc123", roundType: {id: "234dfg345dfg345"}}) {
uuid
}
}
What's the correct syntax in GraphQL to achieve this ?
its cool i got it. just use **update**TestMutate(..) instead of addTestMutate like so:
mutation {
updateTestMutate(input: {uuid: "abc123", roundType: {id: "234dfg345dfg345"}}) {
uuid
}
}
When I work over bot development, I found that messages that comes from user have some field called username, e. g.
{
update_id: 123567890,
message: {
message_id: 1,
from: {
id: 1234567890,
is_bot: false,
first_name: "Kappa",
last_name: "Pride",
username: "kappapride",
}
...
}
Does this field contains an unique value, such as id field? I know, I can make a TG link with it (like #kappapride), but I'm not sure if this field presents for each TG user.
Only one user can have #kappapride (I believe channels and groups can also take it) username at the same time but users can change their username at any time so no, it's definitely not fine to consider it as unique identifier for users.
so my database is as follows
Program1
Posts:
PostsID1:
Name: John
Age: 12
User_Id: nv4h8fh4uen8h8f4ufn
PostsID2
Name: Don
......
I have the user Id for the user, how can i check if that user has post in the database and if he does just return his post rather than all the posts.
For example // nv4h8fh4uen8h8f4ufn is user id.. he has a post so it will return John, 12,
I dont know if I understood right but, the most common is use the users id as a key, and with that generate child keys for his posts, like this:
Users:
nv4h8fh4uen8h8f4ufn1:
Name: John
Age: 12
Posts:
idPost1:
postData
idPost2:
postData
I'm using Alfresco Community 5.0.d and it does not show the fields for company info.
If I search for user then it shows all details but don't let me add company info while creating a user.
So far I came across user.js file at /Applications/alfresco-5.0.d/tomcat/webapps/share/components/console but I'm not able to add the field for new user.
form.addValidation(parent.id + "-create-companyname", Alfresco.forms.validation.mandatory, null, "keyup"); // ADDED this but not showing in form.
form.addValidation(parent.id + "-create-firstname", Alfresco.forms.validation.mandatory, null, "keyup");
form.addValidation(parent.id + "-create-email", Alfresco.forms.validation.mandatory, null, "keyup");
form.addValidation(parent.id + "-create-email", Alfresco.forms.validation.email, null, "change", parent._msg("Alfresco.forms.validation.email.message"));
form.addValidation(parent.id + "-create-username", Alfresco.forms.validation.mandatory, null, "keyup");
form.addValidation(parent.id + "-create-username", Alfresco.forms.validation.nodeName, null, "keyup", parent._msg("Alfresco.forms.validation.nodeName.message"));
Also I have added the key-value in personObj created in users.js as below.
var personObj =
{
userName: username,
password: password,
firstName: fnGetter("-create-firstname"),
lastName: fnGetter("-create-lastname"),
email: fnGetter("-create-email"),
organization: fnGetter("-create-companyname"),
disableAccount: Dom.get(me.id + "-create-disableaccount").checked,
quota: quota,
groups: groups
};
PersonObj is:
personObj
disableAccount : false
email : "test#test.com"
firstName : "test"
groups : Array[0]
lastName : "test"
organisation : "test" //added this key-value
password : "admin"
quota : -1
userName : "test_test"
But company name is not coming. Moreover, I have tried adding multiple user using .csv file link and it does not show the company name (column name is Company as given in guide lines) but do show like mobile number, fax, etc.
Is this is a bug with Alfresco community 5.0.d?
Screenshot of new user form for reference.
I need to add company name field in above form of new user so it could be pre-populated for those new user's.
How could I add the company name field so it gets added to new user's profile?
Thanks
Hi you can add user company details using.
Javascript. example
var x =people.getPerson("admin");
logger.log(x.properties["companyemail"]);
//getting the current Company email
x.properties["companyemail"]="test#google.com";
//setting new company email
logger.log(x.properties["companyemail"]);
//getting the new Company email
companytelephone
companyaddress2
companyaddress1
companyfax
companyemail
companypostcode
These are the some properties of user comapny
To add a field which is already a property of user model.
In case of adding company field, follow below steps:
In users.get.properties file - Add below line.
label.companyname=Company
In users.js file - Added fields company property for not null check and getting it's value followed with clearing the value after a valid entry.
form.addValidation(parent.id + "-create-companyname",Alfresco.forms.validation.mandatory, null, "keyup"); // Add validation
organisation: fnGetter("-create-companyname"), // Get company value
fnClearEl("-create-companyname"); // Clear company field
In users.get.html.ftl file - Added div for company
<div class="field-row">
<span class="crud-label">${msg("label.companyname")}: *</span> </div> <div class="field-row">
<input class="crud-input" id="${el}-create-companyname" type="text" maxlength="100" />
</div>