I am using UUIDs as my primary key in laravel.
The UUID is not generated by eloquent but by a default value in postgresql.
The default value is uuid_generate_v1mc(). This function is from the uuid-ossp package. The id is correctly generated.
But now the problem.
For example, I am creating an user:
$user = User::create(['email' => 'adsf', 'name' => 'adsf', 'password' => 'adf'])
This returns an user model without an id. I've found that I had to do an fresh() on the model to get the default values. But when I do this I get null. I think this is because the fresh function needs the id. But doesn't have it.
The user model has public $incrementing = false;. So that is setup correctly.
How can I get the full user model(In a clean way) when I create it? For the user the email is unique, so I could get the user based on the name. But for other tables this might not work.
Related
I am trying to add some data to a Postgres database using Entity Framework Core via a Http POST method.
The data to be added is passed in the body of my post request which looks like this:
{"id":42,"name":"Hans Musterman","email":"hans#gpost.com", "gender": {"id": 2, "name": "male"}}
Which is exactly the structure I would get returned using a get request.
Still the insert fails with an error:
Duplicate key value violates unique constraint "PK_Genders"
Of course the gender male already exists in the Genders table. What I want to do is add a user to the users table with a gender but referenced for the new user but not new created. What the system seems to do when using DbContext Add is trying to add User and Gender.
Is there a way to do it with a reference? Adding a User with "gender" = null does work.
I think you should have something like genderId or foreign key that reference the gender table in the user model use that foreign key id (genderId) instead of gender.
eg. in you user object
{ ... "genderId": 2 }
Otherwise Entity Framework is going to create a new gender that is why you are getting that error
I have a model Employee and have included a foreign key to store the user object from the res.users table using the following code in my model:
user_id = fields.Many2one('res.users', string='user id', default=lambda self: self.env.user)
In my form, I am already capturing the user object.
Now, I want to display the user object's details (id, name, email) using the foreign key (that is the user object) in my form view.
The <field name="user_id"/> in my form view would display the user name of the stored object. I want to display different fields for id, email.
you can create a related field for user_id,
x = fields.Char(related='user_id.email')
like this you can access the all field values of related record.
I simply cannot wrap my head around Models.
I have two tables, users and companies. Users contains a column called Company which is a foreign key to the companies table. the companies table has two columns, an ID and company_name The value of Company under users references the id in Companies.
I am trying to get the value of company_name through the foreign key like so.
$user = User::find(1)->company->company_name;
My thought process, which may be wrong, is that this (depending on if models are correct) should get the value of the Company column and then using that value, get the company_name from the Companies table.
My models look like so
User
public function company() {
return $this->belongsTo('App\Company', 'company');
}
Company
protected $table = 'companies';
public function user() {
return $this->hasOne('App\User');
}
All I continuously get is Trying to get property of non-object
I can see in the error log it gives that it's actually getting the company name too!
Trying to get property of non-object', 'C:\wamp\www\laravel\app\Http\Controllers\HomeController.php', '41', array('userid' => '1', 'usercompany' => '1', 'company' => 'BMW'))
But I don't understand where it's pullyig userid and usercompany from.
What am I doing wrong?
In this case, you're trying to get the property of a non-object, meaning that either User::find(1) or company is not an object. In the error message provided, it looks like one of the two is an array.
Look at your underlying database tables. Generally, the id is a protected field that is not returned in the result object. usercompany looks like the concatenated field to cross-reference the two tables (i.e. the foreign key).
Is it possible to add additional properties to the User object on the server in WAMS? I would like to store the Id primary key of my User table for (secure) use in my table scripts. At the moment the only id is the vendor specific authentication Id, but I'd like to be able to allow users to choose an authentication method. Currently my (simplified) table design is as follows:
User table:
id
googleId
twitterId
facebookId
name, etc...
League table
id
userId
name, etc
I'd like to store the user primary key in the userId field on the league table, and then query it to ensure that users only get to see leagues they created. At the moment, the user object in table scripts sends through a User object with the Google/Twitter/Windows authentication token and I have to do a query to get the primary key userID, everytime I want to carry out an operation on a table with a userId column.
Ideal solution would be that when the Insert script on my User table is called on registrations and logins I can do:
// PSEUDO CODE
function insert(item, user, request) {
var appUserId;
Query the user table using the user.userId Google/Twitter/Facebook id
If user exists {
// Set a persisted appUserId to use in all subsequent table scripts.
user.appUserId = results.id;
} else {
Set the GooTwitFace columns on the user table, from user.userId
insert the user then get the inserted record id
// Set a persisted appUserId to use in all subsequent table scripts
user.appUserId = insertUserPK;
}
}
Then, in subsequent table scripts, I'd like to use user.appUserId in queries
If all you are trying to do is authorize users to only have access to their own data, I'm not sure you even need the "user" table. Just use the provider-specific userId on the user object to query your "league" table (making sure the userId column is indexed). The values will be provider-specific, but that shouldn't make any difference.
If you are trying to maintain a notion of a single user identity across the user's Google/Facebook/Twitter logins, that's a more complicated problem where you would need a "user" table and the kind of lookup you are describing. We hope to ship support for this scenario as a feature out of the box. It is possible (but fairly messy) to do this yourself, let me know if that's what you're trying to do.
I've made a guestbook application using Google App Engine(GAE):python and the client is running on iPhone.
It has ability to write messages on the board with nickname.
The entity has 3 fileds:
nickname
date
message
And I'm about to make another feature that user can post reply(or comment) on a message.
But to do this, I think there should a 'primary key' to the guestbook entity, so I can put some information about the reply on a message.
With that three fields, I can't get just one message out of database.
I'm a newbie to database. Does database save some kind of index automatically? or is it has to be done by user?
And if it's done automatically by database itself(or not), how can I get just one entity with the key??
And I want to get some advise about how to make reply feature generally also. Thanks to read.
Every entity has a key. If you don't assign a key_name when you create the entity, part of the key is an automatically-assigned numeric ID. Properties other than long text fields are automatically indexed unless you specify otherwise.
To get an entity if you know the key, you simply do db.get(key). For the replies, you probably want to use a db.ReferenceProperty in the reply entity to point to the parent message; this will automatically create a backreference query in the message to get replies.
Each entity has a key, it contains information such as the kind of entity it is, it's namespace, parent entities, and the most importantly a unique identifier (optionally user specifiable).
You can get the key of an entity using the key method that all entities have.
message.key()
A key can be converted to and from a URL-safe string.
message_key = str(message.key())
message = Message.get(message_key)
If the key has a user-specified unique identifier (key name), you can access it like this
message.key().name()
Alternatively, if a key name was not specified, an id will be automatically assigned.
message.key().id()
To assign a key name to an entity, you must specify it when creating the entity, you are not able to add/remove or change the key name afterwards.
message = Message(key_name='someusefulstring', content='etc')
message.put()
You will then be able to fetch the message from the datastore using the key name
message = Message.get_by_key_name('someusefulstring')
Use the db.ReferenceProperty to store a reference to another entity (can be of any kind)
It's a good idea to use key name whenever possible, as fetching from the datastore is much faster using them, as it doesn't involve querying.