ActiveRecord 7 - should destroy! be throwing foreign key violation? - activerecord

I thought using #record.destroy! would return false if it fails to delete the record. However instead it is raising an exception.

Related

Laravel 8 - Unable to seed the DataBase / factory error

Hi I am new to creating factories on Laravel and I am setting up a blog website. I had this going just fine and now it isn't working anymore.
When I use "tinker" to add fake data using (App/Models/Post::factory(30)->create();), below error keep occurs. Could anyone please educate me why this error occurs?
App/Models/Post::factory(30)->create();
PHP Warning: Use of undefined constant App - assumed 'App' (this will throw an Error in a future version of PHP) in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: Use of undefined constant Models - assumed 'Models' (this will throw an Error in a future version of PHP) in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: A non-numeric value encountered in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: A non-numeric value encountered in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: Division by zero in F:\coding\laravel\test-laraveleval()'d code on line 1
[!] Aliasing 'Post' to 'App\Models\Post' for this Tinker session.
Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sint' for key 'categories_name_unique' (SQL: insert into categories (name, slug, updated_at, created_at) values (sint, ut-quisquam-et-et-tenetur-molestias-in-dolor-voluptatum, 2022-03-16 07:11:05, 2022-03-16 07:11:05))
'
You have to define factory rules such that you have a constraint in your database that checks for category name uniqueness.
You should read how to write a custom factory in Laravel 8:Examples how to generate factories.

How to handle delete operation in spring if there are no record in database table?

I am using JdbcDaoSupport in DAO class for database coding in my project and there is a scenario for "deleting a member" from a table. I am using getJdbcTemplate.update("delete MEMBERINFO where memid= "+id); method. But if there is no data in the table, it doesn't raise any exception. The criteria is such that if there are no records in the table and yet user is trying to delete a record, then user will receive an error message saying "No data found". But for that, i need to raise an exception.
I am using oracle 11g XE.
Also same problem for getJdbcTemplate.query("select * from MEMBERINFO");
If there is no data it does not raise any exception.
Actually JdbcTemplate's update method returns int - number of rows was affected. All you need is to check amount of rows returned. If it's 0 the you can show an error message.
As for query it's fine that list is empty. If you need just one object use
queryForObject() method. It throws exception if no result is found.
BTW: The getJdbcTemplate.update("delete MEMBERINFO where memid= "+id); contains SQL injection. Use parameters instead.

Save (possibly) duplicate Sequel Model instance

In Sequel, if you have a Model class called MyModel, you can do something like this:
#make MyModel instance
row = MyModel.new(column1, column2)
#save it to the database Sequel is connected to
row.save
However, "row.save" will fail if you're saving a duplicate primary key.
I'm looking for a method that will save a value to the database if the primary key is not duplicate and save nothing, without failing, otherwise.
I have looked through the instance methods for Sequel Model, but I haven't found what I'm looking for. Is there a way I can do this?
I've not used Sequel before, but you could always wrap the call to save with a begin/rescue block and just swallow the error without doing anything (you almost always want to do SOMETHING).
begin
row = MyModel.new(col1, col2)
row.save
rescue Sequel::DatabaseError => e
# handle error here
end
Note: you might want something other than DatabaseError above.
Sequel distinguishes newly created Model objects and Model objects fetched from the database -- even if they contain the same data. For example, if we get a record like this, saving to the database will succeed:
row = MyModel.where(:column1 => column1, :column2 => column2)
row.save
#save succeeds
If we make a new object with the same values and save it to the database, the save will fail:
row = MyModel.new(column1, column2)
row.save
#save returns duplicate primary key error
To update a value in the database without getting a duplicate primary key error, fetch the record with the relevant primary key (if it exists), modify it, and then save it.

Laravel 4 Sessions error - Duplicate entry '4294967295' for key 'PRIMARY'

I'm using the database session driver in Laravel Four. MySQL DB.
Things were working fine, then I got this error. Trying on different computers all returns the same error.
I truncated the sessions table and reset the auto_increment value. This resolved it for a while, then it happened again.
Looking at the records in the sessions table, the ids would sometimes increment by one, but other times jump by varying amounts, but none near the max value for int(11). Then what appears to be randomly, a session is stored with the max value then everything stops.
Has anyone else seen this kind of error with Laravel Four? Any ideas on whats causing it?
The app is part API and part CMS. Switching to the native sessions driver has resolved the problem so far. But I would like to be able to use the DB driver if I can get it to work.
step 1: select max(your primary_key_field) from your_table_name;
step 2: ALTER TABLE your_table_name AUTO_INCREMENT = value_u_got_from_step1 + 1;
you can put any value instead of 1 instep2...
I think this may work...
Manually reset the auto_increment. Deleting all table rows is not enough.
ALTER TABLE table AUTO_INCREMENT = 1

PLSQL Trigger may be causing INSERT INTO to fail silently?

I have a table which I am trying to do an insert/update on depending on the values I am given. But the insert is not working for this particular table, yet it works for the previous tables which the script has run on.
To test this problem, I put in a few anonymous blocks into oracle's sqldeveloper which inserts or updates depending on whether a key is present. Updates seem to work fine, but when it comes to inserting a new row, nothing is inserted.
If I had this table:
COFFEE_ID TEA_ID NAME
11 100 combo 1
12 101 combo 2
13 102 combo 3
Doing this will not insert anything and will instead move on to the next anonymous block:
begin
insert into COFFEE_TEA(COFFEE_ID, TEA_ID, NAME) values (14, 103, 'combo 4');
exception when dup_val_on_index then
update ....
end;
....
I suspect it has something to do with the trigger on this table. It is a BEFORE EACH ROW trigger type, and it would do an insert data into some other table. There is no exception handling in the trigger, so I'm guessing it must fail but not report it (doesn't show up in sqldeveloper when I run the script).
My two questions would be,
When the trigger runs, what happens if the ID it's trying to insert to the other table already exists? Looks like it silently fails?
How best should I fix this? I am unsure if I can change the trigger code itself, but would it be possible to catch the error inside my anonymous block (assuming that it's actually the trigger that's causing the problem). If so, how would I know what exception to catch if it fails silently?
I removed the exception in sqldeveloper and it tells me that a unique constraint was violated. Namely that the data being inserted into the other table through the trigger is the cause.
Your additional information tells us that your trigger is hurling ORA-00001, a unique key violation. This is the error which the DUP_VAL_ON_INDEX exception handles. So it seems like your exception handler which is supposed to be dealing with key violations on COFFEE_TEA is also swallowing the exceptions from your trigger. Messy.
There are two possible solutions. One is to put decent error handling in the trigger code. The other is to use MERGE for your data loading routine.
I always prefer MERGE as a mechanism for performing upserts, because I don't like using exceptions to handle legitimate expected states. Find out more.
Ideally you should do both. Triggers are supposed to be self-contained code: imposing unhandled exceptions on routines which interact with their tables breaks the enscapsulation.
A trigger will not modify the DML process on a table. Remove the exception block, the insert will either succeed or fail with an error if COFFEE_TEA is a table.
In other words, the following script will never output 0 if COFFEE_TEA is a table:
BEGIN
INSERT INTO coffee_tea(COFFEE_ID, TEA_ID, NAME) values (14, 103, 'combo 4');
dbms_output.put_line(sql%rowcount);
END;

Resources