I have just started working on a project and I am using zend framework and postgresql (normally use MySQL) however I am hitting a problem when I am trying to get the last inserted id when using the Zend_Db insert command.
When using the function $db->lastinsertid('users', 'userid'); I get the following error message:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "users_userid_seq" does not exist LINE 1: SELECT CURRVAL('users_userid_seq') ^
I've checked the database and the sequence does exist, and both the table and the sequence is owned by the the same user that is being use to access the application.
I've even tried $db->lastSequenceId('users_userid_seq'); but still get the same error message.
I am not sure if the problem is with postgresql (I think most likely) or with the framework.
Has anyone else had a similar problem to this?
I see you found your answer, it was simply a typographical error.
FWIW, I'll offer the following suggestion as another reason for the error you saw:
You need to spell the name of the sequence exactly as it is stored, including matching case if the sequence name is stored as anything other than lower-case.
In other words, if the sequence was created with the spelling "Users_userid_seq," but you queried it as "users_userid_seq," this doesn't match and you'll get the error.
Try listing sequences in the psql tool:
postgres=# \ds
This will show you the sequences defined, with their spelling as they are stored in the database.
Check if the schema of the table "users" is in the "search_path" of the Zend_Db session.
The field "userid" is a primary key in the table "users" ?
If it's not, change the userid type.
The following code works for me (PostgreSQL & Zf):
$db->insert($this->getTableName(), $data);
$id = $db->lastSequenceId($this->_sequence);
Replace $this->_sequence with the sequence you are using in the database.
Related
I have a migration which I made at the beginning of my project, basically adding a TEXT column called 'description' which is set to NOT NULL.
Now several months down the track I need to change that to allow null.
I can't use Laravel 5.5 change() function as I have a enum in my column list and it bugs out, so i need to add it as a raw query in a migration like so;
DB::statement('ALTER TABLE `galleries` MODIFY `description` TEXT NULL;');
When i do a php artisan migrate against my local mysql database it all works great, BUT when i try to run my test suite, it all breaks.
Im using SQLite for my test suite, and the error im getting is as follows;
PDOException: SQLSTATE[HY000]: General error: 1 near "MODIFY": syntax error
If anyone else has come up against this issue and fixed it, i would love to hear how you did it.
Thanks
SQLite only allows you to rename the table or add a column. The ALTER TABLE statement cannot change or remove columns.
In order to change or remove a column in SQLite, you need to create a new table with the desired schema, copy the data from the original table to the new table, delete the original table, and then rename the new table to the original name.
This is all abstracted out for you by Laravel and DBAL, so your best bet may be to get help with figuring out the issue with your enum column (though that would be a separate question).
You can read more about altering tables in the SQLite docs here.
I seem to be having an issue when updating records on a specific table.
For reference here is an example of the query that throws an error:
UPDATE `dbname`.`tblname` SET `CustomerID` = '543' WHERE `tblname`.`Issue_ID` = 440
I am able to insert, delete and query rows, as well as update other columns however whenever trying to update the CustomerID field (int, non-null) it throws an error saying:
#1054 - Unknown column 'Revision' in 'field list'
I have all rights to both the database and table however while trying to update the CustomerID column on any rows, ever when Revision isn't even in the query I get the same error.
I looked around a great deal into the issue using a regex in my php code to remove all non-printable characters however even when running the query from phpMyAdmin the same error is thrown.
If anyone has insight into this error it would be greatly appreciated.
Table description:
You may possibly encounter this if you have an update trigger firing off which is referencing a column that does not exist. May be the offending trigger is not even trying to read/write to this table! As such, that column may not exist where it is trying to reference it. Further, you could kick off a cascade of such triggers, and have this buried more than one layer deep.
To show triggers:
http://dev.mysql.com/doc/refman/5.7/en/show-triggers.html
To modify them:
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
I'm trying to update the value in a MYSQL enum field from PHP via Doctrine (5.3 and 1.2 respectively).
I get an error when I try and do this:
$q = Doctrine_Query::create()
->update('StMessages')
->set('status','new')
->where('message_id = ?',$msg_id);
I get a sql state error telling me that the column 'new' does not exist. If I enter 3 instead of new (presumably the internal index of the 'new' value), then the query works. In fact it happens in a SQL client too so perhaps this is a quirk of this version of MySQL? Its 5.1.45.
Anyone know if this is how MySQL is supposed to treat enums or if this is more likely a Doctrine issue? I have 'use_native_enum' set to true.
Given a table
create table test (
enumfield enum('a','b','c')
);
you should be able to do
update test set enumfield='a';
which is the whole point of the num field - not having to mess with indexes and whatnot.
What's the exact definition of your enum field?
I think this was actually an issue relating to the quoting of strings when updating the field's values. I'm not sure if Doctrine or I was at fault.
i echoed the query below: (query is safe)
UPDATE otelozellik
SET isim_tr='test',
aciklama_tr='<p>test1</p>',
uyari_tr='test',
tag_tr='test'
WHERE id='1'
Database Error: Unknown column
'aciklama_tr' in 'field list'
I changed the order of columns, the one after isim_tr keeps giving error. When I move isim_tr to the last then the one after id giving the same error. But moving them to the last position is not a solution for me because table will be dynamic to add new columns when necessary. need an absolute solution.
UPDATE: LATEST SCREENSHOT: http://img5.imageshack.us/img5/7215/mysqlerror.jpg
Solved. Solution is answered below. Thanks everyone.
Problem is solved. Thank you a lot everyone for their help.
Right Query for solution is:
UPDATE `holidaycholic`.`otelbilgi` SET `otelbilgi`.`isim_tr`='test2', `otelbilgi`.`aciklama_tr`='<p>test2</p>', `otelbilgi`.`uyari_tr`='test2', `otelbilgi`.`tag_tr`='test2' WHERE `otelbilgi`.`id`=1
No idea why but that worked for me.
'field list' errors are caused when you try to load data into a field that doesn't exist. Double check that you spelled your field names correctly.
Your post says that you need to add "dynamic columns". A properly structured database shouldn't have a need for that sort of thing. However, if you do want to add columns from php, you need to add them to the table before you try to insert data in those fields. You can use the ALTER TABLE statement to do this:
ALTER TABLE table_name
ADD column_name datatype
Just to double-check are all the characters you're using the standard ASCII characters or are you using an unusual character set?
Try inserting data into the table using phpMyAdmin or similar - If it works, copy the code it generates and run it yourself using the mysql client.
Assuming that still works, compare the generated code with the SQL generated by your PHP
Here is the syntax which works for me all time:
"INSERT INTO table(`code`, `description`) VALUES ('".mysql_real_escape_string($code)."', '".mysql_real_escape_string($description)."')";
"table" is a table with an AUTO_INCREMENT index.
Is there a logical (and possible) way to do something like this?
UPDATE $table SET LIKE %_checkbox = '' WHERE id='$id'
I have fields like allowed_checkbox and types_checkbox and they are sent to the database script dynamically. Can you use a wildcard when referring to the column name?
You've got a bit of a Frankenstein syntax there. The server will need to know the table and column names before compiling the SQL - so you can't do what you're after directly.
Does your php code have no prior knowledge of the database schema?
The key word you used is dynamically - you could find matching column names using a query against the MySQL INFORMATION_SCHEMA.COLUMNS table. You could do this per-update, which would be expensive, or once at application start up extract the schema for all tables you need.
No. You would have to generate the SQL string and then execute it separately. If you're trying to do something like this then you've probably got a bad schema design.