Django ImageField null=True doesn't make blank field equal to NULL - sql

I have a model with this field:
carousel_image = models.ImageField(upload_to='news/%Y/%m/%d, null=True, blank=True)
I was wondering why exclude(carousel_image__isnull=True) and other queries that check if field is or isn't null aren't working.
I checked my sqlite3 db and received this:
sqlite> select carousel_image from asgeos_site_news;
news/2020/12/23/index.jpeg
news/2020/12/23/gradient-1.jpeg
( 3 blank lines )
I also tried adding WHERE carousel_image = NULL and it returned nothing.
Why my images are not null? They're just a blank lines. I have to use carousel_image__exact='' to exclude them right now.

File fields can't be set to null in the db, here's the reference from django documentation
blank = True
is enough.
blank is stored as ''

Related

Get all entries for a specific json tag only in postgresql

I have a database with a json field which has multiple parts including one called tags, there are other entries as below but I want to return only the fields with "{"tags":{"+good":true}}".
"{"tags":{"+good":true}}"
"{"has_temps":false,"tags":{"+good":true}}"
"{"tags":{"+good":true}}"
"{"has_temps":false,"too_long":true,"too_long_as_of":"2016-02-12T12:28:28.238+00:00","tags":{"+good":true}}"
I can get part of the way there with this statement in my where clause trips.metadata->'tags'->>'+good' = 'true' but that returns all instances where tags are good and true including all entries above. I want to return entries with the specific statement "{"tags":{"+good":true}}" only. So taking out the two entries that begin has_temps.
Any thoughts on how to do this?
With jsonb column the solution is obvious:
with trips(metadata) as (
values
('{"tags":{"+good":true}}'::jsonb),
('{"has_temps":false,"tags":{"+good":true}}'),
('{"tags":{"+good":true}}'),
('{"has_temps":false,"too_long":true,"too_long_as_of":"2016-02-12T12:28:28.238+00:00","tags":{"+good":true}}')
)
select *
from trips
where metadata = '{"tags":{"+good":true}}';
metadata
-------------------------
{"tags":{"+good":true}}
{"tags":{"+good":true}}
(2 rows)
If the column's type is json then you should cast it to jsonb:
...
where metadata::jsonb = '{"tags":{"+good":true}}';
If I get you right, you can check text value of the "tags" key, like here:
select true
where '{"has_temps":false,"too_long":true,"too_long_as_of":"2016-02-12T12:28:28.238+00:00","tags":{"+good":true}}'::json->>'tags'
= '{"+good":true}'

MS Access SQL If Then statement for check boxes

I am trying to get a search query to run. It is as follows. The last two lines of code do not work. It is a check box. If the checkboxYes is checked then pull only the items in the YES/NO field of the database that are checked. If the checkboxNo is checked then pull only the items in the YEs/No field of the database that are checked. If BOTH boxes are checked, then pull all items. I cannot get either scenario to work. All other fields except the check boxes work correctly.
SELECT *
FROM NLog
Where
(([Forms]![FrmSearch]![Combo13] is Null) OR ((NLog.State)=[Forms]![FrmSearch]![Combo13])) and
(([Forms]![FrmSearch]![daterecsrt] is Null) or (((NLog.DateRec) Between [Forms]![FrmSearch]![daterecsrt] And [Forms]![FrmSearch]![daterecend]))) and
(([Forms]![FrmSearch]![datesrttxt3] is Null) or (((NLog.DateRec) Between [Forms]![FrmSearch]![datesrttxt3] And [Forms]![FrmSearch]![dateendtxt3]))) and
(([Forms]![FrmSearch]![checkyes] is Null) or ((NLog.Compchk)=True)) and
(([Forms]![FrmSearch]![checkNo] is Null) or ((NLog.Compchk)=False))
Thank you!
I am using MS Acess 2010. This is using the boxes on a form to run the query.
NLog is a table so why are you referencing fields in the table as [Forms]![FrmSearch]![Combo13] and etc. Those are controls on a form and not fields in your table. Shouldn't your query really be:
SELECT * FROM NLog WHERE
(NLog.State is Null OR NLog.State = Forms!FrmSearch!Combo13)
AND (NLog.DateRec is Null OR NLog.DateRec Between Forms!FrmSearch!daterecsrt And Forms!FrmSearch!daterecend
AND (NLog.DateRec is Null OR NLog.DateRec Between Forms!FrmSearch!datesrttxt3 And Forms!FrmSearch!dateendtxt3)
AND (NLog.Compchk is Null OR NLog.Compchk = True)
AND (NLog.Compchk is Null OR NLog.Compchk = False)
Also, the following will not give you any results:
AND (NLog.Compchk is Null OR NLog.Compchk = True)
AND (NLog.Compchk is Null OR NLog.Compchk = False)
NLog.Compchk can not be both True and False. It is going to be one or the other or Null.
You will need to check if the boxes are checked and build the last part of your query around that, (ie IIF()).

sql to set an xml value

I'm a novice in mySql.
I'm trying to replace a value in the xml column of my table.
my select method works.
SELECT * FROM `comics` WHERE ExtractValue(xml,'comic/pageNumber') = 6
my replace method doesn't. I've been searching for the correct syntax for a bit now...
SET xml.modify(
replace value of ('comic/pageNumber') with 5
)
some background:
this situation comes up when i delete a comic page.
it leaves a gap in the page numbers, after which i would either:
iterate through all the comics and remove any gaps in the page numbers.
or
iterate through all comics with pageNumber larger than the deleted page, and reduce their pageNumber by 1.
How about
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>5</pageNumber>')
WHERE ExtractValue(xml,'comic/pageNumber') = 6
Tested on MySQL version 5.1
UPDATE `comics`
SET xml = UpdateXML(xml,
'comic/pageNumber',
concat('<pageNumber>',(ExtractValue(xml,'comic/pageNumber')+1),'</pageNumber>'))
WHERE ExtractValue(xml,'comic/pageNumber') >= 1
You'd be better off actually storing the fields in the table, rather than a single field with xml in it. Then the following would work. Otherwise there's not much point using a relational database at all.
BEGIN;
DELETE FROM `comics`
WHERE `comicID` = :id AND `pageNumber` = :page;
UPDATE `comics` SET `pageNumber` = `pageNumber` - 1
WHERE `comicID` = :id AND `pageNumber` > :page;
COMMIT;

Update colum with value from another column + text

I imported an e-store's database values into my own, and it mostly worked out fine. However, there were no image file names. So, I need to update the entire database- over 6,000 records, so that under 'image' we get a path + model name + jpg, so each product can be associated with an image. Im having trouble mixing the dynamic column value with the static path. Here is what I need to accomplish:
UPDATE `store`.`pr_product`
SET `image` = 'data/products/`**model_no**`.jpg'
WHERE `pr_product`.`product_id` = `pr_product`.`product_id` ;
But cannot get it to recognize the dynamic nature of 'model_no'
Thanks in advance
Max,
Please what you means about dynamic nature of 'model_no'?
Is this column's datatype int or long or varchar
Please need more explaination with example
you can test the following if e.g model_no is column in pr_product table
UPDATE store.pr_product
SET image = 'data/products/'+pr_product.model_no+'.jpg'
WHERE pr_product.product_id = pr_product.product_id ;
Best Regards,
Mohammed Thabet Zaky

SQL query based on criteria

it's working if all the feilds is entered by user. i need a code that combine all the sql command. user may enter in the one field or two field or all the three fields. i need to search the database with one field query or two feild query or three feilds query.
i have try it with where help is a table & search-test is form & contract no,username & date of feild in database and forms.
where the help(table).cont_no(field) is equal or not equal to search-test(form name).cont_no(text box field)
SELECT *
FROM help
WHERE ( forms ! [search-test] ! cont_no = ''
OR help.cont_no = forms ! [search-test] ! cont_no )
AND ( forms ! [search-test] ! username = ''
OR help.username = forms ! [search-test] ! username )
AND ( forms ! [search-test] ! cbo_date = ''
OR help.DATE = forms ! [search-test] ! cbo_date );
I think what you mean is that you want a record included in the result set if the record fields contain a match to the parameters supplied. If a specific parameter is not supplied, then the corresponding field should always be considered a positive match.
That is, if no parameters were supplied, the entire table should be returned.
As for a solution, just a guess (I'm not an MS Access expert, is this for MS Access?), but can you use the iif function to force a match instead? Again, I'm not sure about the syntax, but the underlying logic should work.
Change your WHERE clause to
WHERE
(IIF(forms![search-test]!username is not null, forms![search-test]!username, help.cont_no) = help.cont_no
AND
(IIF(forms![search-test]!cont_no is not null, forms![search-test]!cont_no, help.username) = help.username
AND
(IIF(forms![search-test]!cbo_date is not null, forms![search-test]!cbo_date, help.dbo_date) = help.dbo_date