CREATE TABLE IF NOT EXISTS "access_log" (
"id" BIGINT NOT NULL DEFAULT 'nextval(''access_log_id_seq''::regclass)',
"user_id" BIGINT NOT NULL,
"platform" SMALLINT NOT NULL,
"created_at" TIMESTAMP NULL DEFAULT NULL,
"updated_at" TIMESTAMP NULL DEFAULT NULL,
"device_id" VARCHAR(255) NULL DEFAULT NULL,
"location" VARCHAR(255) NULL DEFAULT NULL,
"ip" VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "access_log_user_id_foreign" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
I am trying to restore my PostgreSQL using HeidiSQL. It is showing invalid input syntax for type bigint.
The call of the nextval() function must not be quoted.
So this:
default 'nextval(''access_log_id_seq''::regclass)'
needs to be:
default nextval('access_log_id_seq'::regclass)
Related
I'm executing below query in postgres 12.11
Table structure as below:
CREATE TABLE pdb_interface.schedule (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
report varchar NOT NULL,
users varchar NOT NULL,
"hour" int4 NULL,
min int4 NULL,
daily_frequency int4 NULL,
daily_workday bool NULL,
weekly_frequency int4 NULL,
weekly_days varchar NULL,
monthly_sected_days varchar NULL,
last_updated timestamp NULL,
deleted bool NULL,
CONSTRAINT schedule_pkey PRIMARY KEY (id),
CONSTRAINT schedule_report_fkey FOREIGN KEY (report) REFERENCES pdb_interface.report("Id"),
CONSTRAINT schedule_users_fkey FOREIGN KEY (users) REFERENCES pdb_interface.users("Id"));
update schedule
set daily_frequency = 12
where id = 'f2f0ba60-f8b1-49ae-a82c-aaddb1a1834b'
The column "id" is of uuid and value is present in the table . However while executing above query, it is giving below error
SQL Error [XX000]: ERROR: no conversion function from character varying to uuid
Is there any work around for the same
I try to generate golang code using sqlc package but it say "the PostgreSQL engine does not support Windows"
I tried to generate code for insert operation in postgresql for which I used postgresql alpine docker image.
my sql.yaml file is
version: "1"
packages:
- name: "db"
path: "./db/sqlc"
queries: "./db/query/"
schema: "./db/migration/"
engine: "postgresql"
emit_json_tags: true
emit_prepared_queries: false
emit_interface: false
emit_exact_table_names: false
account.sql
-- name CreateAccount: one
INSERT INTO accounts (
owner,
balance,
currency
) VALUES(
$1,$2,$3
) RETURNING *;
and my scema contain
CREATE TABLE "accounts" (
"id" bigserial PRIMARY KEY,
"owner" varchar NOT NULL,
"balance" bigint NOT NULL,
"currency" varchar NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "entries" (
"id" bigserial PRIMARY KEY,
"account_id" bigint NOT NULL,
"amount" bigint NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "transfers" (
"id" bigserial PRIMARY KEY,
"from_account_id" bigint NOT NULL,
"to_account_id" bigint NOT NULL,
"amount" bigint NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
ALTER TABLE "entries" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id");
ALTER TABLE "transfers" ADD FOREIGN KEY ("from_account_id") REFERENCES "accounts" ("id");
ALTER TABLE "transfers" ADD FOREIGN KEY ("to_account_id") REFERENCES "accounts" ("id");
CREATE INDEX ON "accounts" ("owner");
What I am missing?
Regarding #282 sqlc doesn't support PostgreSQL for windows. But you can generate files using docker, as mentioned in the installation guide of sqlc.(link)
docker run --rm -v $(shell pwd):/src -w /src kjconroy/sqlc generate
CREATE TABLE "public"."Users" (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now()
);
CREATE TABLE "public"."Boards" (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
"ownerId" SERIAL NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
"updatedAt" TIMESTAMP,
FOREIGN KEY ("ownerId") REFERENCES "public"."Users"(id)
);
CREATE TABLE "public"."Board_Members"(
"userId" SERIAL,
"boardId" SERIAL,
CONSTRAINT board_member_pkey PRIMARY KEY ("userId", "boardId"),
FOREIGN KEY ("userId") REFERENCES "public"."Users"(id),
FOREIGN KEY ("boardId") REFERENCES "public"."Boards"(id)
);
CREATE TABLE "public"."Columns" (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
"boardId" SERIAL NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
"updatedAt" TIMESTAMP,
FOREIGN KEY ("boardId") REFERENCES "public"."Boards"(id)
);
CREATE TABLE "public"."Cards" (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
dueDate TIMESTAMP,
"columnId" SERIAL NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
"updatedAt" TIMESTAMP,
FOREIGN KEY ("columnId") REFERENCES "public"."Columns"(id),
);
I have these tables. Now I want to have a field called "assignee" in "Cards" table which will be a many-to-many relationship with "userId" from "Board_Members" table just like "Boards" and "Users" have a many-to-maany relationship. How do I do that? Do I create a new table and just reference the "userId" column as FK?
Just add a table with 2 foreign keys.
And because I think you want to switch the cards, from person to person. you could also want to keep track of the played history. See also the first normal form to check if your database is designed right.
https://en.wikipedia.org/wiki/First_normal_form
I hope I answered your question :)
In my Laravel 5.6/PostgreSQL 10.5 application
I have 2 tables :
CREATE TABLE public.rt_genres (
id serial NOT NULL,
published bool NULL DEFAULT false,
created_at timestamp NOT NULL DEFAULT now(),
updated_at timestamp NULL,
CONSTRAINT rt_genres_pkey PRIMARY KEY (id)
)...
CREATE TABLE public.rt_genre_translations (
id serial NOT NULL,
genre_id int4 NOT NULL,
"name" varchar(100) NOT NULL,
description text NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
locale varchar(2) NOT NULL,
CONSTRAINT genre_translations_genre_id_locale_unique UNIQUE (genre_id, locale),
CONSTRAINT rt_genre_translations_pkey PRIMARY KEY (id),
CONSTRAINT genre_translations_genre_id_foreign FOREIGN KEY (genre_id) REFERENCES rt_genres(id) ON DELETE CASCADE
)
I need to add slug field in first rt_genres table
with migration rule:
$table->string('slug', 105)->unique();
and got error :
: Unique violation: 7 ERROR: could not create unique index "genres_slug_unique"
DETAIL: Key (slug)=(id) is duplicated.")
1)If there is a way to assign in migration some unique default value, like = id
->default('rt_genres.id')
?
2) That would be cool to assign to slug value from public.rt_genre_translations.name as I use
"cviebrock/eloquent-sluggable": "^4.5" plugin in my app ? Can I do it ?
Thank you!
You can only get the default value from a different column with a trigger (SO answer).
You can make the column nullable:
$table->string('slug', 105)->nullable()->unique();
Or you create the column, insert unique values and then create the index:
Schema::table('rt_genres', function($table) {
$table->string('slug', 105);
});
DB::table('rt_genres')->update(['slug' => DB::raw('"id"')]);
Schema::table('rt_genres', function($table) {
$table->unique('slug');
});
I would like to support PostgreSQL with my extension, however I'm running into a problem when I attempt to install it. Currently, I have the following in my XML for the install section:
<install>
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
<file driver="postgresql" charset="utf8">sql/install.postgresql.utf8.sql</file>
</sql>
</install>
And here is the code for install.postgresql.utf8.sql:
CREATE TABLE "#__shoutbox" (
"id" serial NOT NULL,
"name" character varying(25) DEFAULT '' NOT NULL,
"when" timestamp without time zone DEFAULT '' NOT NULL,
"ip" character varying(15) DEFAULT '' NOT NULL,
"msg" text NOT NULL,
"user_id" bigint(11) DEFAULT 0 NOT NULL,
PRIMARY KEY ("id")
);
INSERT INTO "#__shoutbox" ("name", "when", "msg", "user_id") VALUES ('JoomJunk', '2013-04-04 20:00:00', 'Welcome to the Shoutbox', '0');
I have noticed a few differences between the query for MySQL and PostgreSQL which I think I have taken into consideration, but when I attempt to install the extension, I get the following error:
Database query failed (error # %s): %s SQL=CREATE TABLE "pdo31_shoutbox" ( "id" serial NOT NULL, "name" character varying(25) DEFAULT '' NOT NULL, "when" timestamp without time zone DEFAULT '' NOT NULL, "ip" character varying(15) DEFAULT '' NOT NULL, "msg" text NOT NULL, "user_id" bigint(11) DEFAULT 0 NOT NULL, PRIMARY KEY ("id") );
There is no documentation on supporting PostgreSQL for extensions so I have done what I can by looking at the SQL file from the Joomla 3.1 installation folder.
Is the problem with my query?
The default value for when is not a valid timestamp. If you want when to be empty then remove the NOT NULL constraint so it can be NULL. Otherwise specify a valid timestamp like '2013-4-4 12:34:56'.
Bigint in postgresql does not support a size specification. It is always 64-bits (which is more then 11 decimal digits).
CREATE TABLE "pdo31_shoutbox" (
"id" serial NOT NULL,
"name" character varying(25) DEFAULT '' NOT NULL,
"when" timestamp without time zone,
"ip" character varying(15) DEFAULT '' NOT NULL,
"msg" text NOT NULL,
"user_id" bigint DEFAULT 0 NOT NULL,
PRIMARY KEY ("id")
);