I would like to select all tasks assigned to the current user, where no subtasks are assigned to the current user. This will let me make a report that warns a developer if they are currently accountable for a functional user story, but not working on any technical subtasks related to it.
I thought it would be this (using the Scripted JQL functions):
assignee = currentUser()
and (issueFunction in parentsOf("assignee != currentUser()"))
Unfortunately this subquery does not include unassigned subtasks, despite the assignee presumably being null in this case. I tried experimenting with multiple issueFunction clauses, but that seemed like a bit of a rabbit-hole when what I want is not that complicated.
An example of what I want in SQL would look something like this:
select distinct
t.* from Task t
join
Subtask st using (t.Id = st.TaskId)
where
t.AssigneeId = CurrentUserId
and st.AssigneeId != CurrentUserId
This query does what is needed. It excludes the sub-tasks that has assignee set to current user and then gets all the
other parent tasks.
issuefunction in parentsOf("assignee != currentUser() or assignee is empty")
AND assignee = currentUser()
AND not (issuefunction in parentsof ("assignee = currentUser()"))
Related
Let's assume I have the below structs
type Job struct {
ID string `sql:"type:uuid;primary_key;"`
Title string `json:"title,omitempty"`
Skills []*skill.Skill `json:"skills,omitempty"gorm:"many2many:job_skill;"`
}
type Skill struct {
Name string `json:"name" gorm:"primary_key"`
}
To query all the jobs I do:
jobs := &[]Job{}
gorm.DB.Preload("Skills").Find(&jobs)
How do I search for a Job that contains a certain skill? I have tried the below but it says the column does not exist.
s := "golang"
jobs := &[]Job{}
gorm.DB.Preload("Skills").Where("skill = ?", s).Find(&jobs)
I can see my issues, = doesn't seem to be the correct operator as it needs to search in a list. And it also isn't loading the join table as I assumed it would
Debug output
pq: column "skill" does not exist
SELECT * FROM "jobs" WHERE skill = 'golang'
The Preload method and the Associations feature help you load your fields by constructing basic SQL queries based on the relationships you have created. Queries like loading all skills for a specific job (or jobs). But it doesn't go any more complex than that.
Rather, think of go-gorm as a way to construct your SQL queries and load the data into your models.
Having that in mind, one solution would be to use Joins to include the skill table.
gorm.DB.Preload("Skills")
.Joins("INNER JOIN job_skill js ON js.job_id = jobs.id").
.Joins("INNER JOIN skill s ON s.id = js.skill_id").
.Where("s.name = ?", s).Find(&jobs)
I wonder if it is possible to select by a condition in a preloaded object. For example, I have tables User and Profile (one to one). So I need to get all Users with Profiles where sex is female.
I thought it can be done by something like this:
Preload("UserProfile").Where("user_profile.sex = ?", "female")
But it returns something like:
pq: missing FROM-clause entry for table \"user_profile\"
Preloading doesn't join the tables specified. You need to explicitly join the table in question:
Preload("UserProfile").Joins("LEFT JOIN user_profile ON user.id = user_profile.user_id")
.Where("user_profile.sex = ?", "female")
...Assuming your primary key is called id, and foreign key is called user_id.
I also faced the same issue in a recent project. I found the below solution to work for me.
var users []User
Preload("UserProfile","sex = ?","female").Find(&users)
and then check for
user.profile!=nil
The issue in this approach is it will load all users.
But in your case, it can be another way around.
var profiles []Profile
Preload("User").where("sex = ?","female").Find(&profiles)
I hope this will solve your problem.
I am new to entity framework. I am using a linq query that will fetch many records (upto millions) from database. There are many filter parameters in where condition and on each request the parameters may change. So i wanted to know whether compiled queries will be effective in this case or will it be a new query on each request. Here is my query:
List<FarmerDetailsReport> fdr =
(from fp in mstfp join pd in personalDetails on fp.personDetails.Id equals pd.Id
join ic in identityCertificate on fp.identityCertificate.Id equals ic.Id
join pid in pacsInsuranceData on fp.pacsInsuranceData.Id equals pid.Id into temp
from pid in temp.DefaultIfEmpty()
join bd in bankDetails on fp.bankDetails.Id equals bd.Id
join cd in contactDetails on fp.contactDetails.Id equals cd.Id
join id in incomeDetails on fp.incomeDetails.Id equals id.Id into tmp
from id in tmp.DefaultIfEmpty()
join ua in userAttributes on fp.UserId equals ua.EmailID
where ((ua.CompanyName == companyName ) && (cd.District == model.DistrictForProfileMIS ) && (cd.Block == model.BlockForProfileMIS) && (bd.bankName == model.BankForProfileMIS ) && Status == "Active")
select new FarmerDetailsReport { .......... }).ToList();
Short answer:
Yes...... well, maybe.
Long answer:
This is hard to answer as you have no control over the actual SQL that gets generated.
We had perf problems with some queries like this as the optimizer would optimize for a certain wet of filter cases (like short circuits of clauses) then when a new query was made with a massive change in parameters it would take AGES.
What we did in the end:
Don't use a big LINQ query, create a stored proc or view where you have more control over the SQL generated.
Used things like OPTION(RECOMPILE) ... looks this up it was very useful.
Have a few overloads of the query for different parameters so that the DB can optimize them separately.
Obviously this is just what we did, it might not be perfect for you. I STRONGLY suggest getting the generated SQL for each different parametrized version and going over it with your DBA (if you have one) or your team and google if you don't.
I'm a long time dev, but still kind of new to LINQ. I'm OK when dealing with one set of object, but things get tougher when I need to pull from several sources, and I could use some guidance in getting what I need here.
I have three tables in my database, two related tables and one that holds the PK/FK to tie them together. So something like:
Users
UserID
UserName
Surveys
SurveyID
SurveyName
UserSurveys
UserID
SurveyID
I am using EF and so all of this data has been pulled into Objects.
So... what I want to do is return a List of all Surveys that are associated with a given User. So something like (pseudo-code):
// currentUserID = the UserID I need to get matching Surveys for
var surveys = from Survey where (s => s.SurveyID == UserSurvey.SurveyID && UserSurvey.UserID == currentUserID);
I assume I need to make a sub-query and use a Contains() or something like that, but I keep tripping over myself. Help?
Should be something like this:
from us in UserSurveys
where us.UserId == currentUserID
join s in Surveys on us.SurveyID equals s.SurveyID
select s
If this is EF you should be able to do someUser.Surveys.
Assuming your database and entity model has all of your FK references you should be able to do something like this....
// currentUserID = the UserID I need to get matching Surveys for
var surveys = from s in Survey
where s.User.UserID == currentUserID
select s;
In the table ReservationWorkerPeriods there are records of all workers that are planned to work on a given period on any possible machine.
The additional table WorkerOnMachineOnConstructionSite contains columns workerId, MachineId and ConstructionSiteId.
From the table ReservationWorkerPeriods I would like to retrieve just workers who work on selected machine.
In order to retrieve just relevant records from WorkerOnMachineOnConstructionSite table I have written the following code:
var relevantWorkerOnMachineOnConstructionSite = (from cswm in currentConstructionSiteSchedule.ContrustionSiteWorkerOnMachine
where cswm.MachineId == machineId
select cswm).ToList();
workerOnMachineOnConstructionSite = relevantWorkerOnMachineOnConstructionSite as List<ContrustionSiteWorkerOnMachine>;
These records are also used in the application so I don't want to bypass the above code even if is possible to directly retrieve just workerPeriods for workers who work on selected machine. Anyway I haven't figured out how it is possible to retrieve the relevant workerPeriods once we know which userIDs are relevant.
I have tried the following code:
var userIDs = from w in workerOnMachineOnConstructionSite select new {w.WorkerId};
List<ReservationWorkerPeriods> workerPeriods = currentConstructionSiteSchedule.ReservationWorkerPeriods.ToList();
allocatedWorkers = workerPeriods.Where(wp => userIDs.Contains(wp.WorkerId));
but it seems to be incorrect and don't know how to fix it. Does anyone know what is the problem and how it is possible to retrieve just records which contain userIDs from the list?
Currently, you are constructing an anonymous object on the fly, with one property. You'll want to grab the id directly with (note the missing curly braces):
var userIDs = from w in workerOnMachineOnConstructionSite select w.WorkerId;
Also, in such cases, don't call ToList on it - the variable userIDs just contains the query, not the result. If you use that variable in a further query, the provider can translate it to a single sql query.