cannot get simple PostgreSQL insert to work
Use 'auto dealer'
instead. PostgreSQL interprets "
as being quotes for identifiers, '
as being quotes for strings.
Also:
If this is a new project, just don't use mixed case tables; it is a
source of frustration later. Instead of being able to use any case in
your SQL statements, you must both quote the identifier name and get
the case correct.There is no need to specify
id
/DEFAULT
, you're
asking it to do what it would have done already. I haven't met a DBMS
that requires you to includecolumnName
/DEFAULT
if you want it to
put the default value in the column, so I don't think this extra KV
pair is going to make what is happening clearer to anyone reading
your code later.
Inserting values into a table not working
You must use single quotes for literal string values. Double quoting is interpreted as being an identifier, in this case a column name, which causes the error.
insert into users (id,name,gender,city,age,company)
values (1,'omkar','male','cuttack',24,'tcs');
Cannot insert a simple table into Postgres
Postgres expects its values as a literal string in single quotes. Double quotes are used to refer to case-specific entity names, such as schemas, tables, fields, etc.
In your case, Postgres is trying to find a field called "This is a test", I believe from table quotes, but it doesn't exist. Ergo, 'This is a test'
Postgresql: How to write insert query using where clause
You just need to make sure both users exist in the Users table (execute two queries against the Users table) and then insert a record into the Teams
table.
It's better to use an explicit transaction to get both users and insert a record into Teams.
To get either you have a user in DB you can use count
:
const whereClause = userId ? {
where: {
id: userId,
}
} : {}
const userCount = await db.User.count({
where: {
id: userId
}
})
const userExists = count === 1;
The same way you can check leadId
Postgres insert value from insert in other table
You need a common table expression for this kind of insert chaining:
with ta as (
INSERT INTO tbl_b (status) VALUES ('OK')
RETURNING id
)
INSERT INTO tbl_a (name, tbl_b_reference)
VALUES ('myName', (select id from ta));
Another option is to simply use the lastval()
function to reference the last generated sequence value:
INSERT INTO tbl_b (status) VALUES ('OK');
INSERT INTO tbl_a (name, tbl_b_reference)
VALUES ('myName', lastval());
Note that you must not have any other statements that generate sequence values between those two.
Or use the currval() function:
INSERT INTO tbl_b (status) VALUES ('OK');
INSERT INTO tbl_a (name, tbl_b_reference)
VALUES ('myName', currval('tbl_b_id_seq'));
'tbl_b_id_seq'
is the standard name Postgres uses for a sequence that is created for a serial
column:
Related Topics
How Long Should SQL Email Fields Be
How to Add a Unique Constraint to a Postgresql Table, After It's Already Created
Modify Table: How to Change 'Allow Nulls' Attribute from Not Null to Allow Null
Unexpected Eof Encountered in Bcp
Best Practice to Lock a Record for Editing While Using Entity Framework
MySQL Nested Sets - How to Find Parent of Node
How to Select Top 1 and Ordered by Date in Oracle SQL
How to Add Identity to the Column in SQL Server
Teradata SQL Pivot Multiple Occurrences into Additional Columns
How to Use an Oracle Associative Array in a SQL Query
Is Natural Join Any Better Than Select from Where in Terms of Performance
Join on Set Returning Function Results
SQL Server: How to Select the Installation Path
Drop All Temporary Tables for an Instance