Subsonic 3 InnerJoin - subsonic3

I am not sure how to go about this. I am looking to join columns of two tables together so that instead of just getting a foreign key back, I can display a name that is on the same row as the foreign id.
I did something like this but I kept getting an error about my primary key:
SqlQuery test = db.Select.From().InnerJoin(filesTable.file_typeColumn, filetypesTable.filetype_idColumn).Where(filesTable.file_typeColumn).IsEqualTo(filetypesTable.filetype_idColumn);
Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute
Any suggestions?

The following should get you what you want:
SqlQuery test = db.Select()
.From<filesTable>()
.InnerJoin<filetypesTable>()
You could also do this using SubSonic's Linq implementation:
var fileIdsAndTypeds = from files in filesTable.All()
join types in filesTypes.All()
on types.filetype_idColumn equals files.filetypeColumn
select files;

For some reason that just kept returning a null.
I ended up doing this:
DBDB db = new DBDB();
SqlQuery Files = db.SelectColumns(filetypesTable.filetype_idColumn, filetypesTable.filetype_nameColumn,
filesTable.purpose_idColumn, filesTable.page_idColumn, filesTable.file_idColumn, filesTable.file_descriptionColumn,
filesTable.file_nameColumn, filesTable.file_pathColumn, filesTable.file_typeColumn, filesTable.file_dateColumn)
.From<file>()
.InnerJoin<filetype>(filesTable.file_typeColumn, filetypesTable.filetype_idColumn);
List<file> fileList = Files.ExecuteTypedList<file>();
It seems to return a list of the actual columns on the first table and then inside of that collection is another one with the data from the joined table.... now to figure out how to get that to work with DataBind and custom Gridview columns...

Related

Django doesn't creates objects for all rows in postgresql

I'm scripting a migration tool to get my data from an old software to a new version with different data structures.
While looping through the csv-files I'm creating the objects.
The problem is that every object gets created in postgres but django doesn't.
I'm getting errors like:
duplicate key value violates unique constraint "api_order_pkey"
DETAIL: Key (id)=(159865) already exists.
Order.objects.get(pk=159865) does return "Does not exist"
But a SELECT * FROM api_order WHERE pk = 159865 directly in Postgres finds a row.
with open("order.csv","r") as f:
reader = csv.reader(f)
for row in reader:
Order.objects.create(pk = row[0], ordernr= row[1],....)`
thats of course a short version of it.
On the import of about 250.000 rows this problem appears with about 100 rows.
I had the same problem with deleting. I ran Order.objects.all().delete() but in Postgres there where still a few rows left while Order.objects.all() returned an empty Queryset.
Is there something I can do about this?
Has anyone had a similar problem?
Sorry, my fault.
I had a Manager running which was modifying the queryset.
Thats why Django did not show all records.

Tastypie - Get foreign key resource uri without redundant join query

I'm having Trouble figuring out how to get a resource uri of a foreign key without invoking an Extra Join Query to the database.
Let's say I have the following resource:
QuestionResource(ModelResource):
section = fields.ForeignKey(SectionResource,attribute='section')
What happens is that tastypie generates an extra Join to the Section table in order to populate the "section" field with data (for example).
BUT, I don't need this extra Join in order to populate this field's value (I need only the resource uri), since I already have it in the Question table (section_id) - All I need to do is prefix the 'section_id' field with 'api/v1/section/'.
I can do something like this:
QuestionResource(ModelResource):
section = fields.IntegerField(attribute='section_id')
But I will get only the id, without the prefix of 'api/v1/section/'
I know I can override dehydrate to do it, But I was wondering if there is a built in way in Tastypie to do it , cause it seems like an ugly workaround.

How do you correctly return an aggregate data field using AX 2012 Query Service

I have been working on the AX Query Service as of late. I have a pretty good understanding of everything but it seems that the QueryDataFieldMetadata object does not like aggregates. When I build a QueryDataFieldMetadata object:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Database;
And add it to the data source everything is fine. But when I do this:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Sum;
And add it to the data source the field is not returned at all in the results set. I have checked the datasource itself before executing the query and it is in the fields list but nothing is returned. Does anyone know why this might be happening? Any help would be appreciated.
I just figured this one out. The problem was due to me selecting another field from the table but forgetting to put it in the "Group by" fields. It is strange to me that the query service was returning THAT field with an empty but not returning the aggregate fields at all. Basically I had made a query service query that would be equal to this:
Select wMSLocationId, SUM(AvailPhysical), RecId from InventSum group by ItemId, InventLocationId, wMSlocationId where ItemId == 'some value';
The query was returning:
InventSum.wMSLocationId = 001
InventSum.RecId = 0
The inclusion of the RecId was a mistake, I had forgotten to remove it, but didn't think it would matter as it wasn't in the group by fields and would therefore return null. Removing this selection field did result in the aggregate field returning in the query.
Anyway I hope this helps someone out there as it took me some time to figure out.

Open JPA how do I get back results from foreign key relations

Good morning. I have been looking all over trying to answer this question.
If you have a table that has foreign keys to another table, and you want results from both tables, using basic sql you would do an inner join on the foreign key and you would get all the resulting information that you requested. When you generate your JPA entities on your foreign keys you get a #oneToone annotation, #oneToMany, #ManyToMany, #ManyToOne, etc over your foreign key columns. I have #oneToMany over the foreign keys and a corresponding #ManyToOne over the primary key in the related table column I also have a #joinedON annotation over the correct column... I also have a basic named query that will select everything from the first table. Will I need to do a join to get the information from both tables like I would need to do in basic sql? Or will the fact that I have those annotations pull those records back for me? To be clear if I have table A which is related to Table B based on a foreign key relationship and I want the records from both tables I would join table A to B based on the foreign key or
Select * From A inner Join B on A.column2 = B.column1
Or other some-such non-sense (Pardon my sql if it is not exactly correct, but you get the idea)...
That query would have selected all column froms A and B where those two selected column...
Here is my named query that I am using....
#NamedQuery(name="getQuickLaunch", query = "SELECT q FROM QuickLaunch q")
This is how I am calling that in my stateless session bean...
try
{
System.out.println("testing 1..2..3");
listQL = emf.createNamedQuery("getQuickLaunch").getResultList();
System.out.println("What is the size of this list: number "+listQL.size());
qLaunchArr = listQL.toArray(new QuickLaunch[listQL.size()]);
}
Now that call returns all the columns of table A, but it lack's the column's of table B. My first instinct would be to change the query to join the two tables... But that kind of makes me think what is the point of using JPA then if I am just writing the same queries that I would be writing anyway, just in a different place. Plus, I don't want to overlook something simple. So what say you stack overflow enthusiasts? How does one get back all the data of joined query using JPA?
Suppose you have a Person entity with a OneToMany association to the Contact entity.
When you get a Person from the entityManager, calling any method on its collection of contacts will lazily load the list of contacts of that person:
person.getContacts().size();
// triggers a query select * from contact c where c.personId = ?
If you want to use a single query to load a person and all its contacts, you need a fetch in the SQL query:
select p from Person p
left join fetch p.contacts
where ...
You can also mark the association itself as eager-loaded, using #OneToMany(lazy = false), but then every time a person is loaded (vie em.find() or any query), its contacts will also be loaded.

Left Join with a OneToOne field in Django

I have 2 tables, simpleDB_all and simpleDB_some. The "all" table has an entry for every item I want, while the "some" table has entries only for some items that need additional information. The Django models for these are:
class all(models.Model):
name = models.CharField(max_length=40)
important_info = models.CharField(max_length=40)
class some(models.Model):
all_key = models.OneToOneField(all)
extra_info = models.CharField(max_length=40)
I'd like to create a view that shows every item in "all" with the extra info if it exists in "some". Since I'm using a 1-1 field I can do this with almost complete success:
allitems = all.objects.all()
for item in allitems:
print item.name, item.important_info, item.some.extra_info
but when I get to the item that doesn't have a corresponding entry in the "some" table I get a DoesNotExist exception.
Ideally I'd be doing this loop inside a template, so it's impossible to wrap it around a "try" clause. Any thoughts?
I can get the desired effect directly in SQL using a query like this:
SELECT all.name, all.important_info, some.extra_info
FROM all LEFT JOIN some ON all.id = some.all_key_id;
But I'd rather not use raw SQL.
You won't get a DoesNotExist exception in the template - they are hidden, by design, by the template system.
The SQL you give is what is generated, more or less, when you use select_related on your query (if you're using Django 1.2 or a checkout more recent than r12307, from February):
allitems = all.objects.select_related('some')