has-many does not work - clojure

I've got 2 tables
CREATE TABLE public.user_account
(
id integer NOT NULL DEFAULT nextval('user_account_id_seq'::regclass),
email character(50) NOT NULL,
password character(100) NOT NULL,
CONSTRAINT user_account_pkey PRIMARY KEY (id)
)
CREATE TABLE public.recipe
(
id integer NOT NULL DEFAULT nextval('recipe_id_seq'::regclass),
user_account_id integer NOT NULL,
name text NOT NULL,
description text NOT NULL,
CONSTRAINT recipe_pkey PRIMARY KEY (id),
CONSTRAINT recipe_user_account_id_fkey FOREIGN KEY (user_account_id)
REFERENCES public.user_account (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
This is my clojure declaration
(declare user_account recipe)
(kc/defentity user_account
(kc/entity-fields :email :password)
(kc/has-many recipe {:fk :user_account_id})
)
(kc/defentity recipe
(kc/entity-fields :user_account_id :name :description)
(kc/belongs-to user_account {:fk :user_account_id})
)
And I can not select the user_account table with recipe
user=> (sql-only (kc/select user_account (with recipe) (where {:id 1})))
"SELECT \"user_account\".\"email\", \"user_account\".\"password\" FROM \"user_account\" WHERE (\"user_account\".\"id\" = ?)"

I ran into the same problem. The solution was to addition the pk field to entity-fields list in the parent entity. It really is not intuitive, and seems that the problem is in a poor documentation. So the solution should be:
(declare user_account recipe)
(kc/defentity user_account
(kc/entity-fields :id :email :password)
; ^^^ fix is here
(kc/has-many recipe {:fk :user_account_id})
)
(kc/defentity recipe
(kc/entity-fields :user_account_id :name :description)
(kc/belongs-to user_account {:fk :user_account_id})
)

Related

How to define multiple foreign keys referring to the same table in sequelize or sequelize-typescript?

Defining model for TableC which is a mapping table for TableA and TableB. TableA contains composite primary key (id,globalVersion).So need to define these to fields inside the map table as foreign keys. Tried something as below but its not working.[sequelize-typescript and PostGres]
#BelongsTo(() => TableA)
id_fk: TableA;
#ForeignKey(() => TableA)
#Column(DataType.INTEGER)
id: number;
#BelongsTo(() => TableA)
globalVersion_fk: TableA;
#ForeignKey(() => TableA)
#Column(DataType.INTEGER)
globalVersion: number;

AWS error: Invalid operation: table name "?" specified more than once;

the below code works very well in SQL Server 2012, But when I use it in AWS amazon web service will give me a error "Amazon Invalid operation: table name "#t" specified more than once;"
CREATE TABLE #t (store_id varchar(20),city varchar(20),[state] varchar(20));
INSERT INTO #t VALUES
('22', 'new', 'NY'),
('22', null, null),
('22', null, null),
('33', null, null),
('33', 'LA', 'CA')
;
SELECT DISTINCT store_id, city, [state]
INTO #unique
FROM #t WHERE city IS NOT NULL;
;
UPDATE #t
SET city = #unique.city, [state] = #unique.[state]
FROM #unique
INNER JOIN #t
ON #unique.store_id = #t.store_id
WHERE #t.city IS NULL
Does anyone know why and modify my code? Thank you.
Here you go
UPDATE #t
SET city = #unique.city, [state] = #unique.[state]
FROM #unique
WHERE #unique.store_id = #t.store_id
AND #t.city IS NULL
Redshift does not need target table in FROM clause but in case if you need to specify it you need to alias it.
UPDATE #t
SET city = #unique.city, [state] = #unique.[state]
FROM #unique
JOIN #t t1
ON #unique.store_id = t1.store_id
WHERE t1.city IS NULL
From documentation
If you need to include the target table of the UPDATE statement in the list, use an alias.
https://docs.aws.amazon.com/redshift/latest/dg/r_UPDATE.html

Unable to SELECT COUNT(*) for Korma entity with default fields

I am unable to SELECT COUNT(*) from an entity I have mapped in Korma.
Here is my entity:
(declare users responses)
(korma/defentity users
(korma/entity-fields :id :slack_id :active :token :token_created)
(korma/many-to-many responses :userresponses))
And here is my attempt at a SELECT COUNT(*):
(korma/select
schema/users
(korma/fields ["count(*)"])
(korma/where {:slack_id slack-id}))
I get this error:
ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function at character 8
STATEMENT: SELECT "users"."id", "users"."slack_id", "users"."active", "users"."token", "users"."token_created", count(*) FROM "users" WHERE ("users"."slack_id" = $1)
It looks like Korma is including my entity fields even though I'm specifying fields to select in this query. How do I override that?
You can't override it per se. Korma query manipulation functions are always additive, so specifying fields merely specifies additional fields.
To get around this, you can rewrite this query to select against the users table itself rather than the Korma entity users:
(korma/select :users
(korma/fields ["count(*)"])
(korma/where {:slack_id slack-id}))
But then you'll have to make do without anything else defined in the users entity.
Alternatively, you could rewrite this entity to not define any entity-fields, then define a wrapped version of this entity with the desired default fields:
(korma/defentity users-raw
(korma/many-to-many responses :userresponses)))
(def users
(korma/select
users-raw
(korma/fields [:id :slack_id :active :token :token_created])))```
Then you can write your normal queries by adding with/where clauses to this "users" query, and only directly touch users-raw when you need to exclude those fields:
(-> users (with ...) (where ...) (select))

How to access tables in other schema in korma?

In SQL, accessing tables in other schema is simple:
select *
from other_schema.t
where ...
How can I do this in korma? What I actually to do is to access information_schema.tables table. So defining another db by defdb wouldn't be helpful.
I've tried to define the entity, however, failed.
(defentity information_schema.tables)
I've got to know that there is a way to specify the base table when defining an entity. When specifying the base table, it allows to set the schema with ..
(defentity tables
(table :information_schema.tables))
This works fine for accessing information_schema.tables table, without defining another db.
You should be able to do this by defining another db. I can create a db like this:
CREATE database my_db;
USE my_db;
CREATE TABLE stuff (
things VARCHAR(255)
);
INSERT INTO stuff (things) VALUES ("some things");
Now I define two Korma databases and entities, and query them:
(defdb my-db (mysql {:host "localhost"
:port 3306
:db "my_db"
:user "root"
:password nil}))
(defdb information-schema (mysql {:host "localhost"
:port 3306
:db "information_schema"
:user "root"
:password nil}))
(defentity stuff)
(defentity information-schema)
(select stuff
(database my-db))
;; => ({:things "some things"})
(select TABLES
(database information-schema)
(fields :TABLE_SCHEMA :TABLE_NAME)
(where {:TABLE_SCHEMA "my_db"}))
;; => ({:TABLE_NAME "stuff", :TABLE_SCHEMA "my_db"})

error with query REPLACE using mysql PDO

I try to execute the following mysql query with pdo
REPLACE INTO session SET id = :id, user_id = :user_id, data = :data, timestamp = :timestamp
and I get the following error:
[18-Sep-2014 11:48:10] Exception Message: Unhandled Exception.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, data = ?, timestamp = ?' at line 1.
You can find the error back in the log.:
Query:REPLACE INTO session SET id = :id, user_id = :user_id, data = :data, timestamp = :timestamp
Params:
Array
(
[id] => sv9o264ciicsfd8porp1v0gl46
[user_id] => 0
[data] => version|s:8:"computer";linkedin|a:1:{s:5:"state";s:7:"Q7HXzKo";}github|a:1:{s:5:"state";s:7:"Q7HXzKo";}
[timestamp] => 1411030090
)
My Table session structure is:
CREATE TABLE IF NOT EXISTS `session` (
`id` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`user_id` mediumint(10) NOT NULL,
`data` text COLLATE utf8_unicode_ci NOT NULL,
`timestamp` int(40) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `session` (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
When I use phpmyadmin to execute it is working fine.
Can you help me to understand what is the problem?
well I found it, very silly mistake: I should separate the , from the parameter:
I changed this
:user_id,
to:
:user_id ,
You have forgotten an equal sign.
Change this part of your query:
user_id :user_id
to:
user_id = :user_id
I also think there may be a problem in using a reserved keyword like timestamp for one of your columns, try changing it to something like tstamp. You could also try to quote the name in the query.
REPLACE INTO session SET `id` = :id, `user_id` = :user_id, `data` = :data, `timestamp` = :timestamp