I have an error in Postgresql building a trigger and cannot seem to find a solution - postgresql-14

I am working in PGAdmin4, learning postgresql (yes, I am still green to this), and trying to build a trigger for a project.
Everything seems to be working well until I get to the create trigger:
CREATE OR REPLACE TRIGGER Mail_List_upd
BEFORE UPDATE
ON Mail_List
FOR EACH ROW
EXECUTE PROCEDURE Mail_List_upd_func();
When I execute this, in addition to the rest of my code, I get the following error:
ERROR: syntax error at or near "CREATE"
LINE 73: CREATE OR REPLACE TRIGGER Mail_List_upd
^
SQL state: 42601
Character: 1824
I have gone through all the work and everything looks good. I as curious if this might be a dialect issue, but that does not seem to be it. I looked up another person's work on github and tried it and saw the same error. So I am at a loss.

Related

mysqli_query($conn, $sql) On adding this, giving an error of 500 INTERNAL SERVER ERROR, No detailed error shown [duplicate]

In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error:
Fatal error: Call to a member function bind_param() on a non-object in...
Here is the code:
global $mysqli;
$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");
$stmt->bind_param('i', $cur_id);
$stmt->execute();
$stmt->bind_result($uid, $desc);
To check my query, I tried to execute the query via control panel phpMyAdmin and the result is OK.
TL;DR
Always have mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); in your mysqli connection code and always check the PHP errors.
Always replace every PHP variable in the SQL query with a question mark, and execute the query using prepared statement. It will help to avoid syntax errors of all sorts.
Explanation
Sometimes your MySQLi code produces an error like mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given..., Call to a member function bind_param()... or similar. Or even without any error, but the query doesn't work all the same. It means that your query failed to execute.
Every time a query fails, MySQL has an error message that explains the reason. In the older PHP versions such errors weren't transferred to PHP, and all you'd get is a cryptic error message mentioned above. Hence it is very important to configure PHP and MySQLi to report MySQL errors to you. And once you get the error message, fixing it will be a piece of cake.
How to get the error message in MySQLi
First of all, always have this line before MySQLi connect in all your environments:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
After that, all MySQL errors will be transferred into PHP exceptions. An uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you'll get a conventional PHP error. That will instantly make you aware of the error cause. And the stack trace will lead you to the exact spot where the error occurred.
How to get the error message from PHP
Here is a gist of my article on PHP error reporting:
Reporting errors on a development and live servers must be different. On the development server it is convenient to have errors shown on-screen, but on a live server error messages must be logged instead, so you could find them in the error log later.
Therefore, you must set corresponding configuration options to the following values:
On a development server
error_reporting should be set to E_ALL value;
log_errors should be set to 1 (it is convenient to have logs on a development PC too)
display_errors should be set to 1
On a production server
error_reporting should be set to E_ALL value;
log_errors should be set to 1
display_errors should be set to 0
After that, when MySQL query fails, you will get a PHP error that explains the reason. On a live server, in order to get the error message, you'll have to check the error log.
In case of AJAX call, on a dev server open DevTools (F12), then Network tab. Then initiate the request which result you want to see, and it will appear in the Network tab. Click on it and then the Response tab. There you will see the exact output. On a live server check the error log.
How to actually use it
Just remove any code that checks for the error manually, all those or die(), if ($result), try..catch and such. Simply write your database interaction code right away:
$stmt = $this->con->prepare("INSERT INTO table(name, quantity) VALUES (?,?)");
$stmt->bind_param("si", $name, $quantity);
$stmt->execute();
Again, without any conditions around. If an error occurs, it will be treated like any other error in your code. For example, on a development PC it will just appear on-screen, while on a live site it will be logged for the programmer, whereas for the user's convenience you could use an error handler (but that's a different story which is off topic for MySQLi, but you may read about it in the article linked above).
What to do with the error message you get
First of all you have to locate the problem query. The error message contains the file name and the line number of the exact spot where the error occurred. For the simple code that's enough, but if your code is using functions or classes you may need to follow the stack trace to locate the problem query.
After getting the error message, you have to read and comprehend it. It sounds too obvious if not condescending, but learners often overlook the fact that the error message is not just an alarm signal, but it actually contains a detailed explanation of the problem. And all you need is to read the error message and fix the issue.
Say, if it says that a particular table doesn't exist, you have to check spelling, typos, and letter case. Also you have to make sure that your PHP script connects to a correct database
Or, if it says there is an error in the SQL syntax, then you have to examine your SQL. And the problem spot is right before the query part cited in the error message.
If you don't understand the error message, try to google it. And when browsing the results, stick to answers that explain the error rather than bluntly give the solution. A solution may not work in your particular case, but the explanation will help you to understand the problem and make you able to fix the issue by yourself.
You have to also trust the error message. If it says that number of tokens doesn't match the number of bound variables then it is so. The same goes for the absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.
A list of things you should never ever do in regard of error reporting
Never use an error suppression operator (#)! It makes a programmer unable read the error message and therefore unable to fix the error
Do not use die() or echo or any other function to print the error message on the screen unconditionally. PHP can report errors by itself and do it the right way depends on the environment - so just leave it for PHP.
Do not add a condition to test the query result manually (like if($result)). With error exceptions enabled such condition will just be useless.
Do not use the try..catch operator for echoing the error message. This operator should be used to perform some error handling, like a transaction rollback. But never use it just to report errors - as we learned above, PHP can already do it, the right way.
P.S.
Sometimes there is no error, but no results either. Then it means, there is no data in the database to match your criteria. In this case you have to admit this fact, even if you can swear the data and the criteria are all right. They are not. You have to check them again.
I've got an article that can help in this matter, How to debug database interactions. Although it is written for PDO, the principle is the same. Just follow those instructions step by step and either have your problem solved or have an answerable question for Stack Overflow.

Entity Framework - migrations - #objname is ambiguous

I've bumped into a problem when trying to update my database. I Changed a FK and the virtual property inside my model. When I try to update the database now I'm getting this error:
Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
On this line code it stops the update-database:
RenameColumn("dbo.test", "test_Id", "ProgramId");
And the -verbose attribute is showing me that this line of code is giving me the error:
EXECUTE sp_rename #objname = N'dbo.test.test_Id', #newname = N'ProgramId', #objtype = N'COLUMN'
I don't know why it is giving me this error, does anyone know what is going wrong with the automatically generated migration file?
Thanks, Brent
I tried whay you said in my project which was using EF Core, I didn't get just RenameColumn in migration class, I got :
DropForeignKey(...) ,
DropIndex(...) ,
DropColumn(..)
and then AddColumn(...),
CreateIndex(...) and
CreateForeignKey(...)
and of course worked fine when I updated database.
But I searched for your issue , it seems SQL get confused somehow, the all solutions for problems like you, were saying use [] around tableName or ColumnName, it seems you have another name like these names and SQL can't specify them.
If you just tell me more about your entities before and after changes, I can follow your footstep and get error like you.

Extbase, Uncaught TYPO3 Exception. Flush system cache solves is for a while

Site works fine. Suddenly it will through an error. When clicking "flush system cahce" the error will go away for a some time. Anything from minuttes to severaƦ hours.
Core: Exception handler (WEB): Uncaught TYPO3 Exception: #1247602160:
Unknown column 'tx_my_domain_model_formular.rel_formular' in 'where
clause' |
TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\SqlErrorException
thrown in file
/webserver/typo3/src/6.2.15/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php
in line 862.
It is correct that the columns does not exist. But how to preceed in finding the error after this? Where is the sql generated that causes the site to crash?
UPDATE
A check "myParentObj->getChildObj instanceof myChildObjClass" used to work just fine. After changing myParent model to lazy load the ChildObj, then the class of childObj changed from childObjClass to a lazyLoadClass... I am not sure but I guess that is in turns somewhere somehow caused the core error seen in the log.
How to investigate further I simply cannot work out. Eg. store all DB calls in a file og DB to analyze after the error occours.
First
Please take time to write a good question. For a good question you'll get a good answer. It doesn't have to be perfect (not everyone is a native english speaker) but please reread your question.
Answer
If all fields are defined correctly, just run a DB compare from within the installtool.
If not, you need to define the DB-fields in your extension correctly.
Is this your own extension? When doing the query that triggers the error you could call execute(true) to generate the actual SQL statement and debug from there.

Fully qualified name with dollar sign in SSDT project

I was working on a tsql project, and I have noticed that the existing code used a syntax that I have not seen before. They have put a dollar sign in front of the database name for fully qualified address.
Here is one example :
SELECT c.AccountCode, FROM **[$(SmartAdmin)]**.dbo.Customers c
If I rename the database name as SmartAdmin.dbo.Customers, Visual Studio throws error says "contains an unresolved reference to an object".
It appears to be a Visual Studio related thing, can anyone explain what is this and
whether I can remove it.
Please see the attached screenshots, the last one comes from project solution file.
The [$(SmartAdmin)] syntax is used in an SSDT project to reference objects in other databases. To be precise, it is SQLCMD syntax.
If you have a database reference to SmartAdmin, and if the reference is set up so that [$(SmartAdmin)] properly references it, then that is not the problem. It looks like the problem is the other two references to [SmartAdmin]. Change them to look like [$(SmartAdmin)].dbo.whatever.
Example of why these references are useful: I just edited a stored procedure I had in SSDT to misspell a column name. Within seconds, the column name was underlined in red. I then deleted the database reference the column name depended on. The red underline went away. I added the database reference back, and the column was once again underlined in red. I corrected the column name, and the red underline went away.
Without the database reference, I would have had to wait until the stored procedure was deployed, or possibly executed, to see the error. With the database reference, I found out about the problem in the editor. Just like code.
It appears to me that your database is actually called $(SmartAdmin)
As the error suggests "contains an unresolved reference to an object". which means when you change the database name to SmartAdmin, it cannot find a database with this name on server.
If you want to change the database name you will need to execute the following statement and then you will be able to use that name in your statements.
USE master;
GO
EXEC sp_renamedb '$(SmartAdmin)', 'SmartAdmin';
GO
USE SmartAdmin; --<-- Now you will be able to connect to this database
GO -- without the dollar ($) sign.
This issue relates to Database Project External Database References. When you add another solution to a database project, you can specify the "Database Variable Name", in my case, it is $(SmartAdmin).
Please see this link for more information.
Thanks everybody for answering my question.

PostgreSQL:error reusing the prepared statement in postgresql

Description: i have a problem executing the prepared statement more than once with binding parameters. 1st time it executing correct then resetting the parameters,statement and when i try to execute same prepared statement next time it is failed giving the error like this.
Details: i am executing a prepared statement with binding some parameters ,after executing i am resting the prepared statement and unbinding the parameters. when try to execute the same prepared statement it is giving the error :"27:Error fetching numeric attribute: ColAttribute for this type not implemented yet". i am using libodbc++ library. when i debug it is going wrong at this line(libodbc++ code line)
Line :ResultSet* rs=ODBCXX_OPERATOR_NEW_DEBUG(FILE, LINE) ResultSet(this,hstmt_,hideMe);
Error: "27:Error fetching numeric attribute: ColAttribute for this type not implemented yet"
PostgreSQL version number you are running:
How you installed PostgreSQL:PostgreSQL 9.3.5, compiled by Visual C++ build 1600, 64-bit
Changes made to the settings in the postgresql.conf file: No
Operating system and version:windows 8.1 (64-bit)
What program you're using to connect to PostgreSQL:ODBC 3.5 (libodbc++) library
Is there anything relevant or unusual in the PostgreSQL server logs?:No
For questions about any kind of error:
What you were doing when the error happened / how to cause the error:"27:Error fetching numeric attribute: ColAttribute for this type not implemented yet"
Thanks & Regards
Balakrishna
The issue is with how the libodbc++ handles the parameters and the queries. There are two ways to fix this :
1. Upgrade the libodbc++ to use 7.4(V3) protocol.
2. If you are using old libodbc++, then change disable the UseServerSidePrepare parameter in odbc configuration files.
Explaination for UseServerSidePrepare = 0 in odbc configuration :
The psqlodbc controls more than just whether to use unnamed or named
plans in the server. With UseServerSidePrepare=0, parameters are
handled completely in the driver, replacing the parameter markers with
the values in the query itself.
Note : There may well be applications where UseServerSidePrepare=1
gives a performance hit, especially when running against < 9.2 servers.
It's still a better default IMHO, you can still turn it off if you have to.
PS: I know the question was asked years back, but i recently faced the same issue and had to very less resource which actually worked.