Can't access to django models with an external script - django

I have created a Django project with a series of tables (models) using postgresql. The thing, is that one of them I want to be able to access it also from outside the Django project, because from Django I simply want to see its content, but with an external script I want to insert the data.
The problem I have is that when I try to access any of the tables created in django from an external script, the windows terminal, or even the program that postgresql offers. It tells me that the table does not exist. What am I leaving or doing wrong?
The problem I have is that when I try to access any of the tables created in django from an external script, the windows terminal, or even the program that postgresql offers. It tells me that the table does not exist. What am I leaving or doing wrong?
Below I show a screenshot with the tables I have and how it gives me an error.
As you can see I have the ability to see all the tables, but then it doesn't let me select any of them. I have tried everything with lowercase and neither, removing the prefix Platform_App_ and neither How can I access them?
Here I leave a question that was asked similarly but I can't get it to work.
Thank you.

I will expend answer to be more clear of why this helped.
Short answer: all identifiers without double-quoting are always folded to lower case in PostgreSQL.
Almost that short answer: the main problem is that your table name uses mixed-case table name. PostgreSQL require using double-quotes to make identifier case-sensitive.
So, if your table was named as platform_app_fleet, then this will work:
select * from platform_app_fleet;
because table name is in lower case. But when you have table named with mixing lower and upper cases like Platform_App_fleet - you need to use quoting:
select * from "Platform_App_fleet";

Related

Aggregates in Django and Postgres 9.6 causing invalid SQL

I have a Video and VideoLog model. Until now I've been happy enough annotating on a count to see how many times a video has been watched:
Video.objects.annotate(
views=Count('logs'), # logs is the related name from the VideoLog model
)
This works fine on SQLite but we've just upgraded to Postgres 9.6 and trying to run that gives me
ProgrammingError: column "video_video.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "video_video"."id", "video_video"."name", "video_vide...
Here's the formatted query it's running:
SELECT "video_video"."id",
"video_video"."name",
"video_video"."shorttext",
"video_video"."text",
"video_video"."thumbnail",
"video_video"."product_id",
"video_video"."slug",
"video_video"."available_sizes",
"video_video"."preview_image",
"video_video"."certificate",
"video_video"."download",
Count("video_videolog"."id") AS "views"
FROM "video_video"
LEFT OUTER JOIN "video_videolog"
ON ( "video_video"."id" = "video_videolog"."video_id" )
GROUP BY "video_video"."id"
The confounding thing is that in another project on the same database, with the similar dependencies, both on fresh virtualenvs, this works fine. I guess I'm going to have to pick through this line-by-line until I find something.
I used pgloader to upgrade the database. Turns out it also broke a few things. Not sure where, but it must have been pretty subtle because everything else was working quite well.
I know people rag on the ./manage.py dumpdata → ./manage.py loaddata workflow (somebody in IRC called it a toy yesterday when I asked for a better option) but if your data is sane, it's bloody accurate. It just saved my bacon.

Doctrine: how to set referenceOne relationship without finding() the referenced document?

we need to create a document which references one document in another collection. We know the id of the document being referenced and that's all we need to know.
our first approach is:
$referencedDocument=$repository->find($referencedId);
$newDocument->setUser($referencedDocument);
now the question is if we can do it somehow without the first line (and hitting the database). In the db (we use Mongo) reference is just an integer field and we know that target id, so finding() the $referencedDocument seems redundant.
We tried to create new User with just an id set, but that gets us an error during persisting.
Thanks!
In one of projects I used something like this:
$categoryReference = $this->getEntityManager()->getReference(ProjectCategory::class, $category['id']);
Thou, if you use Mongo, you probably need to use getDocumentManager()
So, link to doctrine docs. mongo odm 1.0.

How to change Sitecore Template field Item Id without data loss?

I recently noticed there is a difference in Item Id for a Sitecore template field between 2 environments (Source and Target). Due to this, any data changes to the field value for the dataitem using the template is not reflecting to target Sitecore database.
Hence, we manually copy the value from source to target and which takes lot of time to sync the 2 environments. Any idea how to change the template field Item Id in Sitecore without data loss in target instance?
Thanks
The template fields have most likely been created manually on the different servers, as #AdrianIorgu has suggested. I am going to suggest that you don't worry about merging fields and tools.
What you really care about is the content on the PRODUCTION instance of your site (assuming that this is Target). In any other environment, content should be regarded throwaway.
With that in mind, create a package of the template from your PRODUCTION instance and the install that in the other environments, deleting the duplicate field from the Source instance. The GUIDs of the field should now match across all environments. Check this into your source control (using TDS or Unicorn or whatever). You can then correctly update any standard values and that will be reflect through the server when you deploy again.
If your other environments (dev/qa/pre-prod) result in data loss for that field then don't worry about it, restore a backup from PROD.
Most likely that happened because the field or the template was added manually on the second environment, without migrating the items using packages, serialization or a third-party tool like TDS or Unicorn.
As #SitecoreClimber mentioned above, you can use Razl to sync the two environments and see the differences, but I don't think you will be able to change the field's GUID, to have the two environments consistent, without any data loss. Depending on the volume of your data, fixing this can be tricky.
What I would do:
make sure the target instance has the right template by installing a package with the correct template from source (with a MERGE-MERGE operation), which will end up having a duplicate field name
write a SQL query to get a list of all the items that have value for that field and update the value to the new field
Warning: this SQL query below is just a sample to get you started, make sure you extend and test this properly before running on a CD instance
use YOUR_DATABASE
begin tran
Declare #oldFieldId nvarchar(100), #newFieldId nvarchar(100), #previousValue nvarchar(100), #newValue nvarchar(100)
set #oldFieldID = '75577384-3C97-45DA-A847-81B00500E250' //old field ID
set #newFieldID = 'A2F96461-DE33-4CC6-B758-D5183676509B' //new field ID
/* versionedFields */
Select itemId, fieldid, value
from [dbo].[versionedFields] f with (nolock)
where f.FieldId like #oldFieldID
For this kind of stuff I sugest you to use Sitecore Razl.
It's a tool for comparing and merging sitecore databases.
Razl allows developers to have a complete side by side comparison between two Sitecore databases; highlighting features that are missing or not up to date. Razl also gives developers the ability to simply move the item from one database to another.
Whether it's finding that one missing template, moving your entire database or just one item, Razl allows you to do it seamlessly and worry free.
It's not a free tool, you can check here how you can buy it:
https://www.razl.net/purchase.aspx

Django doesn't read from database – no error

I just set up the environment for an existing Django project, on a new Mac. I know for certain there is nothing wrong with the code itself (just cloned the repo), but for some reason, Django can't seem to retrieve data from the database.
I know the correct tables and data is in the db.
I know the codebase is as it should be.
I can make queries using the Django shell.
Django doesn't throw any errors despite the data missing on the web page.
I realize that it's hard to debug this without further information, but I would really appreciate a finger pointing me to the right direction. I can't seem to find any useful logs.
EDIT:
I just realized the problem lies elsewhere. Unfortunately I can't delete this post with the bounty still open.
Without seeing any code, I can only suggest some general advice that might help you debug your problem. Please add a link to your repository if you can or some snippets of your database settings, the view which includes the database queries etc...
Debugging the view
The first thing I would recommend is using the python debugger inside the view which queries the database. If you've not used pdb before, it's a life saver which allows you to set breakpoints in your Python script and then interactively execute code inside the interpreter
>>> import pdb
>>> pdb.set_trace()
>>> # look at the results of your queries
If you are using the Django ORM, the QuerySet returned from the query should have all the data you expect.
If it doesn't then you need to look into your database configuration in settings.py.
If it does, then you must might not be returning that object to the template? Unlikely as you said the code was the same, but double check the objects you pass with your HttpResponse object.
Debugging the database settings
If you can query the database using the project settings inside settings.py from the django shell it sounds unlikley that there is a problem with this - but like everything double check.
You said that you've set up a new project on a mac. What is on a different operating system before? Maybe there is a problem with the paths now - to make your project platform independent remember to use the os.path.join() method when working with file paths.
And what about the username and password details....
Debugging the template
Maybe your template is referencing the wrong object variable name or object attribute.You mentioned that
Django doesn't throw any errors despite the data missing on the web
page.
This doesn't really tell us much - to quote the Django docs -
If you use a variable that doesn’t exist, the template system will
insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
set to '' (the empty string) by default.
So to check all the variables available to your template, you could use the debug template tag
{{ debug }}
Probably even better though is to use the django-debugging-toolbar - this will also let you examine the SQL queries your view is making.
Missing Modules
I would expect this to raise an exception if this were the problem, but have you checked that you have the psycopg module on your new machine?

Subsonic 3 SimpleRepository NON Plural Table names?

Is it possible to use SubSonic 3's Simple Repository with non-plural table names? My DB already exists, the table names a re singular add I cannot change them.
Nope, it is hardcoded in the SubSonic's source. You can pull it down and trace the migration steps to see where the plural happens. I know, cause I wanted the same thing.
I was tinkering with modifying the source to make plurals optional via some parameter/config override or alike. But, I didn't get it completed (yet).
If your tables already exist then this is not the intended use of the Simple Repository model. The simple repository model is designed to generate the table structures for you using migrations.
If you are using a database that already exists then you would be better served using the T4 Templates as they also support the relationships between your tables.
Cheers,
Ed
With Subsonic 3.0.0.4 in the settings.ttinclude I removed the line;
AddSingularRule("s$", String.Empty);
which was down about 260 lines in the Inflector rules class. Didn't need to mess around with the subsonic source code.
HTH