Laravel: new records do not start at next id - laravel

Let's say I'm creating a record that has an id of 2. After I deleted that record and I create a new record, the id is 3. I would like it to be 2, since the old one with 2 is gone anyway. How? Or is this not smart to do.
Btw, the id column is a NOT NULL AUTO_INCREMENT
Thanks!

I don't think its a good idea, but still you can use this
If you're using MySQL as your database:
ALTER TABLE table_name AUTO_INCREMENT = 1
this will reset your auto increment column back to 1 or in your case.
you can refer to this documentation if ever.

Related

Spring Boot entity with Pk using Oracle DB Sequence in a trigger

I need a help on persisting an entity in a Oracle DB table that uses trigger and sequence for PK.
By now, I tried these from other stackoverflow questions:
#Id
#GeneratedValue(generator="increment")
#GenericGenerator(name="increment", strategy="increment")
This approach works. It finds the max value of PK and increment the value in 1. But, this doesn't update the Db sequence causing "constraint violation" error at some point.
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_sequence")
#SequenceGenerator(name="id_sequence", sequenceName="MY_DB_SEQ")
This approach doesn't work for me either. Sadly, the Db sequence is not accessible and causing error when it is trying to run internally "select MY_DB_SEQ.nextval from dual". Why is not accesible? Go and ask DB admin :)
It looks like the only option I have is passing null in the Entity PK attribute so that the DB trigger, which uses the a DB sequence to get the nextval when the ID is null, assigns the PK value in DB record.
How can I pass a null value for #Id? Of course this is throwing error because is needed. Is there any other annotation I can use for this?
If this is not possible, what other ways I should try?
Thanks for your help.
UPDATE
I couldn't find another way for this, having a DB Seq that is not accessible for user, and when it requires to pass NULL to PK in order to have DB use the trigger which check NULL value in PK to run seq nexval.
After granting access to DB Seq, this approach of course works.
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_sequence")
#SequenceGenerator(name="id_sequence", sequenceName="MY_DB_SEQ")
Did you create the sequence in the database as below?
CREATE SEQUENCE id_seq INCREMENT BY 1 START WITH 0 MINVALUE 0 MAXVALUE 100 NOCYCLE NOCACHE;
if not create first.
or else add the below properties in the application but it's not good practice.
spring.jpa.hibernate.ddl-auto = update
As of now, I couldn't find any solution to pass NULL to PK in Entity.
In DB, the DB Sequences were granted access. Spring Boot can now read the sequence and gran the next value.

How i can solve ORACLE problem in foreign key

I have a problem in oracle and I need help. I have the following query:
1 CREATE TABLE TEST1 (
2 NAME VARCHAR(20)
3 ID VAR(9)
4 PRIMARY KEY(ID)
5 FOREIGN KEY(NAME) References TEST2(ANAME)
6 ON DELETE CASCADE ON UPDATE SET NULL );
If I want to delete line #6 what should i do?
"How I can change the value of primary key and based of that the foreign keys of this pk will change too?"
First, you should never need to do that. Primary keys like this are really just numbers that identify a row, they have no meaning in themselves. It's like asking how you would change the ROWID of a row.
If you must, you could:
Find the foreign keys pointing to this table and disable them with ALTER CONSTRAINT myconstraint DISABLE
Update your primary table and catch the new id value with UPDATE test1 SET id = mysequence.NEXTVAL WHERE id = :oldid RETURNING id INTO :newid, assuming it's set by a sequence.
Update the ids in your other tables with the new id.
Reenable your constraints.
Note that altering constraints is DDL and will do an implicit commit and this approach will leave your tables unprotected by the foreign key constraints.
A second approach would be to:
Insert a new row in the primary table and catch the new id.
Update the id in the foreign tables with the new id.
Delete the old row in the primary table.
Now that I think about it, that second approach seems better to me. No DDL and it just seems cleaner.

Change Base field odoo 9

i need to import department data to my odoo database, for this odoo can't do this because ID field is read only, all forum tell that i must change this with python module, can you help me to resolve this problem, thank you
my fiend i'm working on a case like yours now this question is not for forums this is a real work you need to create a copy of your database add the ids to every table and fix the relation between them when you finish take every thing to the postgres database :
add id to every table
create many2one field in the new tables than fix it's values by the new id and the old primary key and foriegn key
T1 One------To------Many T2
old_id old_forignkey_T1
AE1 AE1
create copy of this table : add id and many2one_fields
copy_T1 copy_T2
id old_id old_forienkey_T1 many2one_field_id
1 AE1 AE1 null
fix the values of the many2one fields according to the id ,old_id ,old_forign key :
1 AE1 -----> AE1 1
this is really hard work good luck

Auto increment ids in cassandra database

How to create unique id in cassandra column families( like auto-increment id in mysql database ) ?
For unique IDs in Cassandra, you'll want to use UUIDs, which are probabilistically (nearly) guaranteed to be unique. There are a few built-in functions in CQL to help with UUIDs.
You asked for simple query to create table with uuid, so there you are:
create:
CREATE TABLE event (uuid uuid, name varchar);
insert:
INSERT INTO event(uuid, name) values(uuid(), 'john');
select:
SELECT * FROM event LIMIT 1;
to select on name column you must add new index:
CREATE index idx_event_name ON event(name);
and now you can select with where:
SELECT * FROM event WHERE name = 'john';
If you really need integer autoincrement IDs I've written a simple python module that does that after going through stackoverflow and not seeing anything decent that does that specific function. If you don't care about the ID being an integer you're better off using something like UUID which is probably safer and more elegant.
link: https://github.com/qdatum/globalcounter

How can I insert to Cassandra with CQL, to table with only primary key, using UPDATE?

I need to insert new rows to Cassandra, to a table that has only primary key columns, e.g.:
CREATE TABLE users (
user_id bigint,
website_id bigint,
PRIMARY KEY (user_id, website_id)
)
The obvious way to do it would be by INSERT:
INSERT INTO users(user_id, website_id) VALUES(1,2);
But I want to do it with use of Hadoop CqlOutputFormat and CqlRecordWriter only supports UPDATE statements. That's usually not a problem as UPDATE is in theory semantically the same as INSERT. (It will create rows if given primary key does not exist).
But here... I don't know how to construct UPDATE statement - it seems that CQL just does not
support my case, where there are non-primary key columns. See what I tried:
> update users set where user_id=3 and website_id=2 ;
Bad Request: line 1:18 no viable alternative at input 'where'
> update users set website_id=2 where user_id=3;
Bad Request: PRIMARY KEY part website_id found in SET part
> update users set website_id=2 where user_id=3 and website_id=2;
Bad Request: PRIMARY KEY part website_id found in SET part
> update users set website_id=2,user_id=1;
Bad Request: line 1:40 mismatched input ';' expecting K_WHERE
Some ideas on how to resolve it?
Many thanks.
Not sure if you can do this with update like that. But why not just create a new dummy column that you never use for anything else? Then you could do
update users set dummy=1 where user_id=3 and website_id=2;
You can't update primary key values in Cassandra as you have explained. As a solution you could also delete the row and insert a new one with the correct value in it. It's just a bit cleaner than creating two rows with one incorrect.

Resources