activerecord adding NULL as 'id' field? - ruby

I am creating a user via:
user = User.create attrs
Basically, it screws up by adding a 'id' field with value NULL which I am guessing evaluates as 0. So after the first user, I get an error that id must be unique. I can confirm that the attrs hash does not contain an 'id' field.
This is the generated SQL:
INSERT INTO `users` (`birthday`, `created_at`, `device_token`, `device_type`, `email`, `fb_id`, `first_name`, `gender`, `last_name`, `location`, `rstatus`, `updated_at`) VALUES ('1992-02-28 00:00:00', '2012-12-06 17:43:43', NULL, 'x86_64', 'adfas023#gmail.com', '12593646569', 'Test', 'male', 'Once', Test, 'Single', '2012-12-06 17:43:43')
What's going on?
Thanks

Your id column should be auto-incrementing, and your migrations would normally ensure this is so. If you can't/aren't using migrations, then fix it manually.

Related

Unknown Column when using where clause in set relation n-n

when I use set relation n-n with where clause like this
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname', null, $this->db->like('actor.actor_name','Katja' , 'both');
this is not working (Unknown column 'actor.actor_name' in 'where clause') but when I put in the where clause column from the first table it would work. like this
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname', null, $this->db->like('film.film_name','taken' , 'both');
So I cannot access the fields from the selected table!!
I need to search the name of the actors, Could someone please help
Is there a shorter way as using the actor's table as the main table, and then filter it with $crud->where()?
thanks

Create and insert table

I made a table for database and the table created but the insert vales are not working.
This is the table
Create table patient (
Patient_ID Number(9) primary key,
First_name varchar2(15),
Last_name varchar2(10),
Contact number(10),
City varchar2(20),
Doctor_ID Number(9) references Doctor(Doctor_ID));
This is the insert statement
insert into patient values ('21345', 'John', 'Smith', '111-111-1111', 'NJ');
insert into patient values ('21346', 'Emily', 'Rose', '222-222-2222', 'LA');
insert into patient values ('21347', 'Mark', 'Cruise', '333-333-3333', 'NY');
insert into patient values ('21348', 'Bran', 'Stark', '444-444-4444', 'TX');
insert into patient values ('21349', 'Hailey', 'Wraith', '555-555-5555', 'AZ');
I am getting an error saying not enough values.
You are only inserting 5 values when your table is expecting 6 (ParentID, First Name, Last Name, Contact, City and Doctor ID)
You need to pass in a value for Doctor_ID
You forgot to add the table column names during the insert so it tries to add all the data your passing to each column in the table. This is a better way of entering your data when it doesn't require you to insert into all table columns.
Insert into `patient` (`table1`, `table2`, `table3`, `table4`, `table5`) values ('21345', 'John', 'Smith', '111-111-1111', 'NJ');
To break down your issue more, you have 6 columns in your table, but the data your passing is 5, it will give you this error because 5 is less than 6. If you don't want to receive this error you need to state each column you are entering into as seen above -- BUT that field will need to be a nullable field.
In this case Doctor_ID is missing
Insert into `patient` (`table1`, `table2`, `table3`, `table4`, `table5`, `table6` ) values ('21345', 'John', 'Smith', '111-111-1111', 'NJ', 'DOCTOR_ID_DATA_HERE');
ANSWER:
Insert into `patient` (`Patient_ID`, `First_name`, `Last_name`, `Contact`, `City`, `Doctor_ID`) values ('2125', 'John', 'Doe', '111-111-1111', 'LA', '30114');
DOCTOR 30114 needs to already exist because you are referencing from another table, please take note!

Replace $request->all() with only non empty form fields

is it possible to replace $request->all() in the below line with only fields that are not empty in submitted form.
$product = Product::create($request->all());
when some form fields are empty, the query generated is something like
insert into `products` (`name`, `companyname`, `ip`, `host`, `status`, `language`, `updated_at`, `created_at`) values (efesar, ewrewrewre, , , , , 2018-04-18 10:29:11, 2018-04-18 10:29:11))
And that's returning error.
This would get you only the fields with values:
$data = collect($request->all())->filter()->toArray();
$product = Product::create($data);
But you should get from the request only the fields which are appropriate for your Product model or better yet, add validation for them. Depending on what you need, you might have to change some your table fields to be nullable to be able to save empty fields.
Its not a problem to submit null fields. Just make them nullable in migration file:
$table->string('ip')->nullable();
$table->string('host')->nullable();
$table->string('status')->nullable();
$table->string('language')->nullable();

DB::insert - returns no error, yet no inserts result from statement

I have created a backup of a given table using the following raw queries:
DB::statement("DROP TABLE IF EXISTS answers_bup");
DB::statement("CREATE TABLE answers_bup AS TABLE answers");
The answers table has the following schema:
CREATE TABLE answers
(
id uuid NOT NULL,
user_id uuid NOT NULL,
survey_id uuid NOT NULL,
question_id uuid NOT NULL,
answer character varying(255) NOT NULL,
created_at timestamp(0) without time zone NOT NULL,
updated_at timestamp(0) without time zone NOT NULL,
}
Now, in order to restore a single row, from the answers_bup table to the answers table. I wrote the following DB::insert:
$void = DB::insert("INSERT INTO answers
SELECT
'?'::uuid AS id,
'?'::uuid AS user_id,
'?'::uuid AS survey_id,
'?'::uuid AS question_id,
answer,
created_at,
updated_at
FROM
answers_bup
WHERE
id='?'::uuid", [
$newId,
$user_id,
$survey_id,
$question_id,
$answer->id
]);
Basically, I only need to copy over three fields from the answers_bup table - answer, created_at and updated_at. The others, have to be assigned new values, hence the above statement.
When I run this code fragment, I get no errors. Yet, the insert does not happen. The answers table remains empty.
Could anyone help me understand what might be wrong here?
Try DB::statement(), or DB::table('answers')->insert('...')

cx_Oracle.DatabaseError: ORA-00947: not enough values

I have a table:
create table employee (
employee_id NUMBER NOT NULL,
name VARCHAR2(255) NOT NULL,
notes VARCHAR2(4000),
created_by varchar2(255) not null,
created_at date default sysdate not null,
updated_by varchar2(255) not null,
updated_at date default sysdate not null,
PRIMARY KEY(vendor_id)
);
so when I insert from SQL developer:
insert into employee(employee_id, name,notes) values(1,'xyz','test');
it auto populates create_by, created_at, updated_at and updated_by.
row gets inserted successfully.
Whereas if I try to insert using cx_Oracle module in python,
cursor.execute("INSERT INTO employee VALUES (:employee_id,:name,:notes)",
{
'employee_id' : max_value,
'name' : each_vendor,
'notes' : 'test'
}
)
it throws error saying not enough values.
Why do I get this error? How can I solve it?
The answer is very simple, and has nothing to do with python. Your 2 insert statements are very different.
In the 1st, you explicitly name the columns you intend to provide values for: (employee_id, name,notes). However, in the insert statement used from Python, you don't specify the 3 columns by name. As a result, your insert statement expects you to provide the values for all columns in the table.
The fix: explicitly name the 3 columns:
cursor.execute("INSERT INTO employee (employee_id, name, notes) VALUES (:employee_id,:name,:notes)",
{
'employee_id' : max_value,
'name' : each_vendor,
'notes' : 'test'
}
)

Resources