How can I from django's ORM get elements that are not in common between two tables.
For example I have a table 1 with a primary key c1 and a table 2 with a column c1.
I tried to do this but I get an error
Table1.objects.exclude(c1=Table2.objects.all()).values_list('c1', flat=True)
*** ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
Related
For simplicity, let's assume that I have two tables: A and B.
Table A has a JSONB field called data with the field columns.
This JSONB array is just a list of ids that are primary keys in Table B.
Table B has the field id (the others don't matter for the question).
The idea is to create a constraint so that it's impossible to delete a row in Table B if this row's id is IN A.data->'columns'.
As far as I know, it's impossible to create such constrains in a conventional way so I have decided that this behavior can be implemented as:
SELECT *
FROM B
WHERE id = ANY (SELECT UNNEST(ARRAY(
SELECT JSONB_ARRAY_ELEMENTS_TEXT(A.data -> 'cols') FROM A)::int[]
)
);
This query supposedly does exactly what I want, but it looks clumsy enough to assume that there must be a fancier way. Constructing an array and then unnesting it doesn't seem to be optimal anyway.
Can you think of a better way to achieve the behavior I described above?
I have a fact table with many entries, and they have 'ship to' columns that are very closely related, but none of the columns are always unique. I would like to make a dimension table for this, and reference the new dimension table rows using a key.
I can create the new dimension table with a create table as select distinct, and I can add a row number primary key for it, but I'm not sure how to put the matching foreign key into the fact table, where they match.
I could easily create a new foreign key column, and fill it using a where to match the old distinct rows in the fact table to the distinct rows in the dimension table, but there is no easy column to match (since there is no key yet), so do I need to create a 'where' match that matches all of the columns together, then assigns the primary key from the dimension table?
I may just be lazy, and don't want to research how to create alter queries and how to create complex where matching, but it seems like a pretty common action for database management, so I feel like it might help others.
I guess one way to do it is to create a concatenation of all of the values in all of the columns for each row in the new dimension to make a unique identifier from the data, then do the same thing for the columns in the fact dimension. Now there is a unique key between the two, and this can be converted to an integer id. Add a new sequence column in the new dimension, then create a new column in the fact, and set it to the integer id in the new dimension where the concatenated id is the same.
This is obviously very inefficient, as the whole contents of the new dimension have to be duplicated, as well as that again in the fact dimension - just to create a link.
I have the following relationship set up between my fact table and dimension tables.
When trying to create a data mining structure, I had to choose the dimension table Dimension_Status as a nested table for the fact table as I'm trying to predict the probability of "TimelyResponse" in the fact table using the "IssuedVia" in the Dimension_Status table. But when trying to do so, I get the following error.
Dimension_Status table cannot be used as a nested table because it does not have a many-to-one relationship with the case table. You need to create a many-to-one relationship between the two tables in the data source file
What am I doing wrong here? Why am I getting this error though my dimension tables are maintaining a many to one relationship with the fact table? Please advice.
I could be completely missing the mark here (I haven't done a great deal of data-mining using SSAS), but from what I can tell nested tables are the "Many" side of a many-to-many relationship. From the MSDN article on Nested Tables it shows the "Products" table as being nested in the "Customer" table, because each Customer can have many Products:
In this diagram, the first table, which is the parent table, contains
information about customers, and associates a unique identifier for
each customer. The second table, the child table, contains the
purchases for each customer. The purchases in the child table are
related to the parent table by the unique identifier, the CustomerKey
column. The third table in the diagram shows the two tables combined.
A nested table is represented in the case table as a special column
that has a data type of TABLE. For any particular case row, this kind
of column contains selected rows from the child table that pertain to
the parent table.
So it looks like nested tables are not what you're after - unfortunately I'm not familiar enough with the SSA data mining tools to recommend the appropriate approach (unless switching them around and making the DimStatus table your Case table and Fact_CustomerComplaints your Nested table will work in your situation.)
To put it simply, your arrows are backwards.
Reverse the relationships so the tables you want to be nested are pointing to your Fact_ table.
Like so:
This is for SQL Server 2008.
I have a master lookup table that looks like this:
Mstr_lookup_ID *Lookup_ID* *Lookup_Category* Lookup_Value
1 1 States CA
2 2 States NY
3 1 Airlines SWA
4 2 Airlines United
5 3 Airlines American
I have my primary key on Lookup_ID and Lookup_Category. I have a table that where I would like to restrict values in certain columns based upon the combination of Lookup_ID and Lookup_Category.
Create Table Main_Table
(State_id INT --ONLY VALUES ALLOWED SHOULD BE 1-2
Airline_id INT) --ONLY VALUES ALLOWED SHOULD BE 1-3
Is there a way to accomplish this neatly? I'd like to create a foreign key for it, but my primary key is on two columns. I'd appreciate any thoughts or suggestions.
See Five Simple Database Design Errors You Should Avoid.
What you're trying to do is their point #1.
It sounds like a great idea - at first. Having just one table instead of many ...
But it's really not, precisely because it defeats the main purpose of a lookup tables - being able to enforce referential integrity.
Don't do this - use a separate lookup table for each category and use proper referential integrity!
The only way you could make this work would be to have a INT IDENTITY on this "global" lookup table - but that ID would be "global" across all categories. Then you could reference this global lookup table based just on that one ID. The downside is that your categories won't all have nice consecutive numbers - it'll be all over the place.
So, you have a "one big lookup table"?
To create a FK (and there is no other declarative way to restrict values based on lookup table, otherwise you can use triggers) you have to add another column to the main_table ie the lookup_category column, and draw the reference using both columns to the primary key in the lookup table.
I have two views: one is a normal view built from one table with an integer ID and other columns for the record (let's call it View1). I have another View (View2), which has an integer ID column and a second column named "table" (type: varchar). That second column contains the name of the table to which the ID column is related: So, if View2 contains an ID of 999 and its "table" column contains the value "View1", that means the record referenced is ID 999 from View1.
Far as I can tell, DBML only allows for one-to-one or one-to-many relationships based on explicit column references; I'd rather express the relationship as a one-to-one based on the ID column AND View1.table being equal to "View2".
Is this possible? I know I can simply do an outer join in the linq query, but I'd rather avoid that if possible. Thanks!
It's not possible. The linq2sql mapper allows for mapping explicit foreign key relations, but if you don't actually have a foreign key relationship in the database, it's not possible for L2S to "infer" the relation in any way.