Django how to select foreign value in query - mysql

Having following models:
User(models.Model):
...
login = ...
Asset(models.Model):
user = models.ForeignKey(User)
...
How to select users login in Asset query using django QuerySet capabilities. For example:
Asset.objects.extra(select = {'user_login' : 'user__login'})
make to return query set with user_login field in each model object

Each Asset object already has a foreign key to the user. So you can always access -
asset = Asset.objects.get(pk=any_id)
if asset.user.login == 'some_value':
do_some_magic()
Please read the documentation.

Use .select_related('user') to select all assets and related users in a single query. Then simply access it through asset.user.login.
assets = Asset.objects.selec_related('user').filter(<any filter>)
for asset in assets:
# no additional queries here, as the user objects are preloaded into memory
print asset.user.login

I have found following solution:
Asset.object.extra( select = {'user_login' : '`%s.%s`' % (User._meta.db_table, 'login') } ).order_by('user__login')
The order_by expression is used to make JOIN on User's model table, than user's login can be accessed in SELECT expression within user_table.login

Related

Filter for records that exist as foreign key in multiple table

I have Django models set up like below.
class Person:
first_name
last_name
# More_fields
class Service1:
person(fk=Person)
# Service1_specific_fields
class Service2:
person(fk=Person)
# Service2_specific_fields
class Service3:
person(fk=Person)
# Service3_specific_fields
# More_services
I'm trying to create an "advanced search" view that will allow users use checkboxes to query the Person model for records that have services matching the checked boxes.
E.g. If checkboxes for services 1, 2, and 3 are checked, results should only be of Persons that have all three services.
Currently it is set up to get all objects from all 3 services and compare them to one another. As expect, this is extremely slow.
What's a better approach for this?
What about:
( Person
.objects
.filter( service1__pk__isnull = False )
.filter( service2__pk__isnull = False )
. and so on
)
You can use Q() to combine just checked conditions.

Sequelize - return single column from associated table as custom column

I have two models: User and Images. User has profile_image_id column.
When i get the user with include {model:Images, as:'profileImage', attributes:['filename']} I get profileImage as object with filename as property.
Is there a way in Sequelize to get that 'filename' as a property of User model?
Meaning to execute
SELECT u.id, u.name, i.filename
FROM users u
LEFT JOIN images i ON i.id = u.profile_image_id
WHERE u.id = 1
What works for now is defining VIRTUAL attribute profileImageFIlename on user and then populating it in afterFind function of User model. But it seems like a lot of extra work and unnecessary data.
Is there a better way except raw query?
The short answer is that there isn't a way to do it that would be "less work". Even in your example SQL query you reference i.filename using the alias that you created for the related images table. This effectively maps to User.images.filename, which is just as usable as User.profile_image_file.
If you would like profile_image_id to exist as a VIRTUAL field on User then you are doing it the correct way - VIRTUAL fields won't be persisted to the database schema, so you would need to set them from some other source. In this case, the related images table provides the value and you will need to set it in the afterfind() hook.
If you don't care about it being on the User Instance or Model and just want to access the value in the results without having to traverse the object, you can use something like the following to alias the column by leveraging Sequelize.literal().
User.findById(1, {
attributes: {
include: [[Sequelize.literal('images.filename'), 'profile_image_file']],
},
include: [{ model: Images, as: 'images', attributes: [] }]
})
.then((user) => {
// There will be a user.dataValues.profile_image_file value populated
// but not a user.profile_image_file unless you set it in afterFind()
console.log(user.dataValues);
});
This will result in SQL of
SELECT `user`.`id`, `user`.`name`, images.filename AS `profile_image_file`
FROM `user` AS `user`
LEFT OUTER JOIN `images` AS `images` ON `user`.`profile_image_id` = `images`.`id`
WHERE `user`.`id` = 1;

Add attribute to ActiveRecord_Relation results based on existence of relationship?

I have a users table, a notifications table, and a join table of notification_reads. I'm trying to write a scope for an ActiveRecord query that will return all of the notifications (Notification.all) with an additional field based on whether the user has a correlating notification_read for that notification.
I'm imagining it would look something like:
class Notification
scope :with_reads_by_user, -> (user) {
select("*", "[some sql that produces a boolean] as read")
.joins(:notification_reads)
.where(notification_reads: {user: user})
}
end
Nothing I've tried has seemed to come close, though.
Try the below one,
scope :by_user, lambda { |user|
joins("LEFT OUTER JOIN notification_reads ON notification_reads.user_id = ? and notification_reads.notification_id = notifications.id", user.id).select("*", "IF(notification_reads.user_id IS NULL, FALSE, TRUE) as is_read_by_user")
}
Query
Notification.by_user(current_user)
Ref: select column as true / false if id is exists in another table
P.S: Not tried this.

Symfony 2 Self referencing many to many repository

I have a self referencing many to many relationship on my User entity being they can have many followers or follow many other users.
I am now trying to write a query in the user repository which will determine if a user is following another user.
I tried to write the query directy on user_relations (the mapping table) but it would not let me as it not related to the user entity.
So I tried:
$query = $this->createQueryBuilder('u')
->select('count(u.id)')
->innerJoin('u.following', 'r')
->where('u.id = :userID')
->where('r.from_id = :followingID')
->setParameter('userID', $userId)
->setParameter('followingID', $followingId)
Which results in an error stating the user entity does not have a field named from_uid.
How the hell can I correctly achieve this?
You can use MEMBER OF doctrine keyword
$query = $em->createQuery('SELECT u.id FROM User u WHERE :followingID MEMBER OF u.following');
$query->setParameter('followingID', $followingId);
$ids = $query->getResult();

How to get all or specific properties of a user from a database?

I'm new to grails and I would like to know how to get all the properties of a login user in an application. I've used MYSQL db to store its information such as username, passowrd, address, etc. I would like to know how to get the specific value of its properties from the database and display it on a textfield/gsp. Please help. tnx..
If you'd like to fetch list of user object with pagination, you can use:
def users = User.list(params)
where params is map (hash) of request (CGI) parameters.
params should contains offset and max.
If you'd like to fetch user by id:
def users = User.get(<ID>)
or
def user = User.findById(<ID>)
Or by some other property:
def user = User.findBy<PROPERTY_NAME>(<PROPERTY_VALUE>)
Each of these method should return user object with all fields.
In gsp, we can display it as:
${user?.username}
or
${fieldValue(bean: user, field: "username")}