I have the following Model:
class SystemMessage(Model):
subject = TextField(default=None)
message = TextField(default=None)
And the following output from explain
| id | int(11) | NO | PRI | NULL | auto_increment |
| language_code | varchar(5) | NO | | NULL | |
| subject | longtext | NO | | NULL | |
| message | longtext | YES | | NULL | |
I have no outstanding migrations or makemigrations
Is there any reason why message is NULLable but subject is not? I would like both to be NOT NULLable.
I'm provided a default value for each field to ensure IntegrityError when saving the strings with no value.
Related
I am new to PowerBI. I am trying to implement the following scenario in PowerBI
I have the following 2 tables -
Table 1:
| ExtractionId | DatasetId | UserId |
| -- | --- | --- |
| E_ID1 | D_ID1 | sta#example.com |
| E_ID2 | D_ID1 | dany#example.com |
| E_ID3 | D_ID2 | dany#example.com |
Table 2:
| DatasetId | Date | UserId | Status |
| --| --- | --- | --- |
| D_ID1 | 05/30/2021 | sta#example.com | Completed |
| D_ID1 | 05/30/2021 | dany#example.com | Completed |
| D_ID1 | 05/31/2021 | sta#example.com | Partial |
| D_ID1 | 05/31/2021 | dany#example.com | Completed |
| D_ID2 | 05/30/2021 | sta#example.com | Completed |
| D_ID2 | 05/30/2021 | dany#example.com | Completed |
| D_ID2 | 05/31/2021 | sta#example.com | Partial |
| D_ID2 | 05/31/2021 | dany#example.com | Completed |
I am trying to create a PowerBI report where, given an extraction id (in a slicer), we need to identify the corresponding DatasetId and UserID from Table 1 and use those fields to filter Table 2 and provide a visual of user status on the given date range.
When I am trying to implement the above scenario, I am creating a Many-Many relationship between DatasetID columns of Table1 and Table2, but cannot do the same for UserID column simultaneously as I get the following error :
You can't create a direct active relationship between Table1 and Table2 because an active set of indirect relationship already exists.
Because of this, given an extractionId, I can filter on DatasetID but not UserId and vice versa. Could you please help me understand what mistake I am doing here and how to resolve the same?
Thanks in advance
This case you said too. You can only merge two or more columns. Than you will create relationships.
My access pattern/query would be: Get Name and Email of all friends by user (example USER#1).
I have added GSI to invert PK and SK which will allow me to query using SK.
Sample data is below in table,
+--------+----------+------+---------------+
| PK | SK | Name | Email |
+--------+----------+------+---------------+
| USER#1 | USER#1 | Bob | bob#email.com |
| USER#2 | USER#2 | Rob | rob#email.com |
| USER#3 | USER#3 | Tom | tom#email.com |
| USER#1 | FRIEND#2 | | |
| USER#1 | FRIEND#3 | | |
+--------+----------+------+---------------+
My question is it possible to get friends of USER#1 in single query?
Thanks in advance.
Option 1: Denormalization
Store the data in the following format:
+--------+----------+------+---------------+
| PK | SK | Name | Email |
+--------+----------+------+---------------+
| USER#1 | USER#1 | Bob | bob#email.com |
| USER#2 | USER#2 | Rob | rob#email.com |
| USER#3 | USER#3 | Tom | tom#email.com |
| USER#1 | FRIEND#2 | Rob | rob#email.com |
| USER#1 | FRIEND#3 | Tom | tom#email.com |
+--------+----------+------+---------------+
Pro: can query and get user name and email directly
Con: update becomes expensive
Option 2: BatchGetItem
Store the data as it is, aka
+--------+----------+------+---------------+
| PK | SK | Name | Email |
+--------+----------+------+---------------+
| USER#1 | USER#1 | Bob | bob#email.com |
| USER#2 | USER#2 | Rob | rob#email.com |
| USER#3 | USER#3 | Tom | tom#email.com |
| USER#1 | FRIEND#2 | | |
| USER#1 | FRIEND#3 | | |
+--------+----------+------+---------------+
Then use BatchGetItem API to retrieve the name and email for each friend.
Pro: data is "normalized" (less duplication, less storage)
Con: you need to do BatchGetItem's in a for loop if you want to retrieve all the friends' details
For e.g., in this table, I'd like to be able add the "description" text at the Django ORM layer and have it reflected at the database level.
test=# \d+ django_model
Table "public.django_model"
Column | Type | Modifiers | Description
--------+---------+-----------+-------------
i | integer | |
j | integer | |
Indexes:
"mi" btree (i) - Tablespace: "testspace"
"mj" btree (j)
Has OIDs: no
I suppose you can't do it. Here's the https://code.djangoproject.com/ticket/13867 request. Closed 6 ya as "Won't do".
You still can use postgres COMMENT extension, eg:
t=# create table t (i int, t text);
CREATE TABLE
Time: 12.068 ms
t=# comment on column t.i is 'some description';
COMMENT
Time: 2.994 ms
t=# \d+ t
Table "postgres.t"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+----------+--------------+------------------
i | integer | | | | plain | | some description
t | text | | | | extended | |
My question is similar to Django Advanced Filtering but I need another approach:
Abstract:
Tables: manufacturer, supplies
Manufacturers have multiple supplies (1 or 0 in "supply" table)
I have a HTML form with multiple (20+ checkboxes) which should limit the queryset with AND queries (so standard). The HTML checkbox names equal MySQL field names. My table looks like this:
mysql> explain supply;
+----------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| manufacturer_id | int(11) | NO | MUL | NULL | |
| supply1 | tinyint(1) | NO | | NULL | |
| supply2 | tinyint(1) | NO | | NULL | |
| supply3 | tinyint(1) | NO | | NULL | |
| [...] | tinyint(1) | NO | | NULL | |
| supply20 | tinyint(1) | NO | | NULL | |
Now in pseudo SQL, I'd like to:
User selected checkboxes supply2 and supply14: SELECT * FROM supply WHERE supply2 = 1 AND supply14 = 1;
User selected checkboxes supply1, supply9 and supply18: SELECT * FROM supply WHERE supply1 = 1 AND supply9 = 1 AND supply18 = 1;
I'm pretty sure I need some QuerySet with kwargs, but I'm unable to construct the view for my needs (still learning Django).
I wonder if the data model here couldn't use some tweaking? You might want to have a supply table with twenty rows and an intermediate table connecting them (that is a ManytoMany(Supply) or something like that). Then you could just have a multi select field, rather than 20 check boxes (unless you really need them for some other reason).
If you need to add another supply, it's simply adding another row, rather than a schema migration.
supplies = Supply.objects.filter( supply1 = 1 )
And if you want to filter again:
supplies = supplies.filter(supply2 = 1)
The filter() method returns a QuerySet, so you can chain as many filter() calls as you like.
I have the following database and when I try to run the query that has been shown below I get the error that says Cannot add or update a child row. I do not understand why am I getting this error.
The object_id that I am entering (327) exists in the database. I have tried this with other object_ids as well and the same error comes up. What is happening here?
like_objects
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| object_id | int(11) | NO | MUL | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
+-------------+---------+------+-----+---------+----------------+
objects
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| object_name | longtext | NO | | NULL | |
| object_desc | longtext | NO | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| likes | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
Here's the query and the error it produces:
insert into like_objects (object_id,user_id) values (327,1)
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`test_db`.`like_objects`, CONSTRAINT `object_id_refs_id_57f96810` FOREIGN KEY (`object_id`)
REFERENCES `objects` (`id`))
This is very strange because I have used a similar like logic for another table and it is working absolutely fine.
Important I have obtained objects table from a mysqldump. Initially the objects table was created form PhpMyAdmin and the like_objects table has been created by the django models
I have figured out what the problem was. As I have posted in my question that the objects table was created using phpmyadmin and then dumped into my database which is part of a django project.
So the storage engine of the initially created table was MyISAM, but the tables created by django were having storage engine innodb. So as result whenever I tried to insert an object in the like_objects table which referenced the objects table there was this incompatibility of the the storage types.
This link helped me to find out what was the storage engine for my table and then I changed the MyISAM engine to innodb using the belwo given command and it is working fine.
ALTER TABLE products ENGINE = innodb