Insert into postgres table with select and autoincrement - postgresql

I want to move data from a table T1 to another table T2. T1 has an autoincrement id, which is consequente of the JPA annotations #Id #GeneratedValue(strategy = GenerationType.SEQUENCE) with Spring Data implementation.
insert into T1(id, dataColumn)
(select NULL, dataToCopy
from T2)
This doesn't work, as it reports an error on the violation of the non-null constraint on the id field. How can I insert my data in this case?

The reason I cannot make the insert that way is that autoincrement is not set on the id column. The reason for not having autoincrement is that setting the a generator for the id leads to the need of making an additional select statements to get the next value from a database sequence.

Related

Auto update primary key when insert into existing table?

Actually my question is basically the same as here but I need to insert values from a nested query into an existing table and I'am not that familiar with sql as to know how to incorporate the setval part into my query:
insert into table_a
select *
from table_b
where val_1 IN (select "val_1" from table_c where "val_2" is null)
returns
ERROR: duplicate key value violates unique constraint "table_a_pkey"
DETAIL: Key (qid)=(470971) already exists.
Now I know I could do a workaround with drop column and autgenerate as described here but there must be a more elegant way. I am using Postgresql/Postgis 2.4 btw.
It he primary key is auto-generated, don't insert the PK column:
insert into table_a (some_column, other_column, third_column)
select some_column, other_column, third_column
from table_b
where val_1 IN (select "val_1" from table_c where "val_2" is null)
(I had to guess the column names as you did not provide the real ones)

Postgres Partitioning not working with hibernate if id is bigserial

I have partitioned my table in Postgres. So there are 2 tables now :
Base table users , with no primary key but using a sequence generator for id column : nextval('users_id_seq'::regclass)
Child table inheriting users
CREATE TABLE users_part_2019_01 (
CHECK (createdon >= '2019-01-01 00:00:00'
AND createdon < '2019-02-01 00:00:00')
) INHERITS (users);
ALTER TABLE users_part_2019_01 ADD CONSTRAINT users1_pkey PRIMARY KEY (id);
I am inserting data into users table using jpa. In the data model I have used :
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Getting the following error :
"org.springframework.orm.jpa.JpaSystemException: The database returned
no natively generated identity value; nested exception is
org.hibernate.HibernateException: The database returned no natively
generated identity value
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)"
It is working as expected independently with postgres.
Got the solution. Instead of GenerationType.IDENTITY, GenerationType.AUTO is working
In case you are coming here from spring boot (2.2.x +) and using postgresql partitioning, the accepted answer alone is not enough to make it work. Hibernate, in this case, will throw the following:
Schema-validation: missing sequence [schema_name.hibernate_sequence]
In this case, the hibernate simply wants you to provide the identity generator for the sequence field by passing it as:
#GeneratedValue(strategy = GenerationType.AUTO, generator = "schema_name.generator_sequence_name_seq")

JPA Generated Values

I generated a web CRUD application following this page
But the wizard does not annotate primary key autoincrement field in the table(s) with #GeneratedValue.
And of course, when run the UI does not populate the autoincrement ID field with value.
So I tried looking at this page
I tried annotating the ID field(s) with #GeneratedValue but with no success.
Do I need to create table with autoincrement column first?
http://www.binarytides.com/create-autoincrement-columnfield-in-apache-derby/
CREATE TABLE students
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(24) NOT NULL,
address VARCHAR(1024),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;
this works...
then in UI remove the field for ID.

inserting data into multiple tables using procedure in oracle

I have 4 Tables
Customer(cust_id (PK), cust_phone, name)
Order((order_id,cust_id) (PK), order_date, order_amt)
Order_line((order_id,Product_id)(PK), qty)
Product(product_id, Product_name, product_price)
The order table has a composite key saying an order only exists with a customer.
The Order_line table has a composite key as it is a bridge table between product and order table. the order_amt is a calculated column, its the total order value.
The question is how do i insert data into the order table and the Order_line table? using procedure, triggers or a temp table or any other way? I am really stuck at this. Help!!
Do I need to make any changes in the model?

T-Sql: How to write a SP that executes several similar insert statement?

I have this table called DevProject which keeps track of developers who are affected to a particular project.
ID int PK
DevID int NOT NULL FK
ProjectID int NOT NULL FK
ID is the PK column for DevProject table. DevID and ProjectID are respectively PK in the Developers and Project tables. When A new project starts, a group of developers are affected to that project. Some of the developers might still be working on other projects. So, After the manager has decide whose going to work on a particular new project, the DevProject table should be populate with the ID of the new project and the ID of the developer.
Thanks for helping.
I know what you meant. And that link had the answer.
declare #ID int;
insert into table_1 (value) values ('newProj2');
SET #ID = SCOPE_IDENTITY();
insert into table_2 (id, memberID) values (#ID, 1);
insert into table_2 (id, memberID) values (#ID, 2);