Zend_Db_Table_Abstract - should we use our table column names? - zend-framework

Zend_Db_Table_Abstract insert method will accept an array of $data containing column value pairs.
Should that column names inside the array, correspond, exactly, to our database table column names ?

Yes.
On the array passed we NEED to make sure our keys name on that array correspond, exactly, to our table column names.

Related

PostgresQL: Value is only allowed in one of two columns

I have a database with two tables "Config" and "Config_xml", each consisting of the same columns (id, content, modifier, etc...). The only difference is, that config only contains non-xml strings in its content column, whereas config_xml contains an xml string in its content column.
Now I'd like to combine these two tables into one, providing a content column and an xml_content column, to simplify querying, because at the moment I always have to query on both tables.
Now is there a way to constrain each row to allow a value in either content or xml_content?
Thanks in advance.
You can use a check constraint that requires one column to be null.
alter table the_table
add constraint check_content
check (num_nulls(config, config_xml) = 1);
To also avoid empty strings, you might want to use:
check (num_nulls(nullif(trim(config::text), ''), nullif(trim(config_xml::text), '')) = 1)

PostgreSQL get array column value from second index

I have a table with a column that contains array of numbers like : ['1','3','45'];
And was looking for a way to select the column value from the second index so that I get ['3','45']
Is there any way to do this in PostgreSQL?

how to change the length of varchar of array in postgresql database without losing data

I tried with the code below, but raised a "syntax error at or near array". Googled around, and nothing found. Is it possible to do so? Thanks!
alter table "tablename" alter column "columnname" TYPE ARRAY(VARCHAR(200));
It's unclear to me if you want to increase the length of each entry, or the length of the array.
An array declaration follows the form datatype[] - the [] makes the column an array and the data type specification is the one for the base type.
So, if you want to increase the length of each array element, just declare an array with a longer varchar length: varchar(200)[]:
alter table "tablename"
alter column "columnname" TYPE varchar(200)[];
If you want to use the ARRAY keyword, that needs to be put after the data type:
alter table "tablename"
alter column "columnname" TYPE varchar(200) array;
If you want to increase the length of the array (=allow more array elements) you don't need to do anything because even if you did specify an array dimension, this is not enforced by Postgres

PostgreSQL: COPY from csv missing values into a column with NOT NULL Constraint

I have a table with an INTEGER Column which has NOT NULL constraint and a DEFAULT value = 0;
I need to copy data from a series of csv files.
In some of these files this column is an empty string.
So far, I have set NULL parameter in the COPY command to some non existing value so empty string is not converted to NULL value, but now I get an error saying that empty string is incorrect value for the INTEGER column.
I would like to use COPY command because of its speed, but maybe it is not possible.
The file contains no header. All columns in the file have their counterparts in the table.
It there a way to specify that:
an empty sting is zero, or
if there is en empty string use the default column value?
You could create a view on the table that does not contain the column and create an INSTEAD OF INSERT trigger on it. When you COPY data into that view, the default value will be used for the table. Don't know if the performance will be good enough.

Rename column without breaking functions

Is there a way to rename a table column such that all references to that column in existing functions are automatically updated?
e.g. Doing this
ALTER TABLE public.person RENAME COLUMN name TO firstname;
would automatically change a reference like the following in any function:
return query
select * from person where name is null;
Since function bodies are just strings, there is no way to automatically change references to columns in function bodies when you rename a column.