SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name - laravel-8

The following migration:
Schema::dropIfExists('personal_access_tokens');
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->id();
$table->morphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
$table->index(['tokenable_type', 'tokenable_id']);
});
Results in the following error:
SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key
name 'personal_access_tokens_tokenable_type_tokenable_id_index' (SQL:
alter table personal_access_tokens add index
personal_access_tokens_tokenable_type_tokenable_id_index(tokenable_type,
tokenable_id))
Why is this happening and how do I fix the issue?

You first need to drop the created index tokenable_type and tokenable_id in order to re-create the table.
Schema::table('personal_access_tokens', function ($table) {
$table->dropIndex(['tokenable_type', 'tokenable_id']); // Drops indexes
});
After you did that you can drop the table and create it again.

Related

SQL Error [42704]: ERROR: large object xxxxxxx does not exist

I had a table with large object.
When I want to delete a row. I have an error: SQL Error [42704]:
ERROR: large object 123456 does not exist.
I checked in pg_largeobject and I didn't find a row with id = '123456'.
How can I delete an row which has a nonexistent object?
The trigger on the table is
CREATE TRIGGER t_filledreport BEFORE UPDATE OR DELETE ON rep_reportjob
FOR EACH ROW EXECUTE PROCEDURE lo_manage(filledreport);
There are two options:
temporarily disable the trigger:
ALTER TABLE rep_reportjob DISABLE TRIGGER t_filledreport;
DELETE ...;
ALTER TABLE rep_reportjob ENABLE TRIGGER t_filledreport;
As superuser, tempoarily set session_replication_role to replica:
BEGIN;
SET LOCAL session_replication_role = replica;
DELETE ...;
COMMIT;
Caution! With triggers disabled, you can easily introduce inconsistencies!

PostgreSQL accessing nested uppercased properties

I've recently switched from mongoDB to postgreSQL and for a while I got stuck with this problem - I can't seem to find a way to access nested property of an object. Yes, I shouldn't be having uppercased column/table names to start with, but I really want to keep naming consistency.
Let's say I have the following db table:
users {
ID: bigint
}
Now let's say I want to get deleted user(old) ID, how do I access this ID?
create or replace function deleted()
returns trigger AS
$body$
begin
perform pg_notify('deleted', ----->WHAT GOES HERE<-----);
return new;
end;
$body$
I've tried filling in placeholder with old."ID", then I get following error:
ERROR: function pg_notify(unknown, bigint) does not exist
LINE 1: SELECT pg_notify('deleted', old."ID")
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT pg_notify('deleted', old."ID")
CONTEXT: PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42883
If I try doing this -> old.ID then I get
ERROR: record "old" has no field "id"
CONTEXT: SQL statement "SELECT pg_notify('deleted', old.ID)"
PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42703
Also I've tried this: `"old.ID", then I get the following:
ERROR: column "old.ID" does not exist
LINE 1: SELECT pg_notify('deleted', "old.ID")
^
QUERY: SELECT pg_notify('deleted', "old.ID")
CONTEXT: PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42703
Thank you for your patience.
pg_notify() wants two arguments of text datatype. So you can cast your bigint argument to that datatype:
pg_notify('deleted', (old."ID")::text)

ERROR: syntax error at or near ";" at CREATE TABLE

I study Postgresql. I've just installed PG on Ubuntu, checkout as user "postgres" and run psql commandline. But when I try to execute commands like SHOW, CREATE and so on, I get:
ERROR: syntax error at or near ";"
postgres#comp:/etc/postgresql/10/main$ psql
psql (10.5 (Ubuntu 10.5-1.pgdg18.04+1))
Type "help" for help.
postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=# CREATE TABLE TEST1
postgres-# ;
ERROR: syntax error at or near ";"
LINE 2: ;
^
postgres=# CREATE TABLE TEST1;
ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE TEST1;
^
postgres=# CREATE TABLE 'TEST1';
ERROR: syntax error at or near "'TEST1'"
LINE 1: CREATE TABLE 'TEST1';
^
postgres=#
I tried to find the same issues on Stackoverflow, but no result. What did I do wrong?
This error occurs because the table structure is not specified. You should execute something like this:
CREATE TABLE TEST1 (id bigserial primary key NOT NULL);
More information https://www.postgresql.org/docs/current/sql-createtable.html

CREATE SCHEMA using a name stored

I am trying to create a new schema on my postgres database which name is stored on an existing table, my query look like this:
CREATE SCHEMA (SELECT name FROM table)
But I am getting a syntax error. What am I doing wrong? Is this a valid way to create the new schema? Which other solution exist for this issue?
You can do it with dynamic sql:
do
$$
declare s_name text;
begin
-- make sure there is exactly one row in that table!
-- otherwise you need some where condition or an aggregat to ensure that.
SELECT name INTO s_name FROM some_table;
execute 'create schema '|| quote_ident(s_name);
end;
$$
It is not possible, create schema syntax required a valid schema name (i.e. valid schema name string). in your case it will throw exception as
********** Error **********
ERROR: syntax error at or near "("
SQL state: 42601
Character: 15

Oracle trigger failed -ORA-04098

I have a table for which i have written a trigger:
CREATE OR REPLACE TRIGGER ac01_control_trigg
AFTER INSERT ON ac1_control_test
FOR EACH ROW
DECLARE
BEGIN
IF :NEW.cur_pgm_name = 'LSN'
AND :NEW.nxt_pgm_name ='MD'
AND :NEW.file_status='RD' THEN
INSERT INTO ac1_control_test
(FILE_NAME, FILE_PATH,FILE_STATUS,CUR_PGM_NAME,NXT_PGM_NAME)
VALUES
(:NEW.FILE_NAME, :NEW.FILE_PATH,:NEW.FILE_STATUS,:NEW.CUR_PGM_NAME,'MD_MPS');
END IF;
END ac01_control_trigg;
when i am trying to insert into the table i am getting an error below!
ORA-04098: trigger 'CNGDB18.AC01_CONTROL_TRIGG' is invalid and failed re-validation
could anybody please help?
also when i compile the trigger in Toad,i am getting compile errors as below:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/65 PLS-00049: bad bind variable 'NEW_FILE_STATUS'
but what is the wrong with this?
and what does this error mean?
EDIT: Now that we see the message, the solution is easy :)
Use :NEW.file_status='RD' instead of:new_file_status='RD'
Your trigger object is invalid (there is a problem with the code).
Test this with:
SELECT object_name, status
FROM user_objects
WHERE object_name = 'AC1_CONTROL_TRIGG';
Should return:AC1_CONTROL_TRIGG INVALID
You can try the following in SQL*Plus to get a description of the error:
ALTER TRIGGER ac1_control_trigg COMPILE;
SHOW ERROR TRIGGER ac1_control_trigg;
Using TOAD, you can just type these two lines into an editor, select them and use Editor>Execute SQL via SQL*Plus.