I'm currently writing a plugin for a customer, and while it's usually working good, I found that dbDelta does not allow me to create the table I need on plugin activation.
I'm running the below code to bind the activation function:
register_activation_hook(__FILE__, 'adminInstallation');
And this is the function itself:
function adminInstallation(){
global $wpdb;
$objectEquipment = 'wp_object_equipment';
$equipmentSQL = "CREATE TABLE ".$objectEquipment." (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL
);";
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
$equipment = dbDelta($equipmentSQL);
}
Once this has been run, I am checking the database, but no tables have been added. Trying to dump the error will only result in Wordpress telling me there was unexpected output, but it wont let me see the actual message that the server returns. This issue has been bugging me for some hours, and I cant continue until it's solved. Does anyone here have any idea why it might do this?
As far as I can tell, all the code is valid, and this is the third plugin I've written. I even tried using the code from my previous ones, but that did not work either.
EDIT: I tried running the function after the plugin activation and dump the dbDelta response. It reports that the table has been created, but still, there's nothing new in the database. Any ideas?
Thanks in advance! //
Jonathan
From http://codex.wordpress.org/Creating_Tables_with_Plugins:
You have to put each field on its own line in your SQL statement.
You have to have two spaces between the words PRIMARY KEY and the definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
You shoud echo $wpdb->last_error; after dbDelta call so you get the mysql error.
You can try this function:
$table_name = "ratings";
$table_columns = "id INT(6) UNSIGNED AUTO_INCREMENT,
rate tinyint(1) NOT NULL,
ticket_id bigint(20) NOT NULL,
response_id bigint(20) NOT NULL,
created_at TIMESTAMP";
$table_keys = "PRIMARY KEY (id),
KEY ratings_rate (rate),
UNIQUE KEY ratings_response_id (response_id)";
create_table($table_name, $table_columns, $table_keys);
I search and dbDelta is running $wpdb->query($sqlQuery); for custom queries.
For those whose dbDelta is not working use
dbDelta($sql_query_to_create_table);
Thanks!
Have a nice code
I do know that dbDelta is very particular about the formatting. I believe the issue is that you are not using backticks (`) around database parts. Try the following.
On a side note, wouldn't hurt to designate a primary key.
$equipmentSQL = "CREATE TABLE `".$objectEquipment."` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL,
PRIMARY KEY (`id`)
);";
Related
I'm facing issue in Query that was written through normal CakePHP version 2.3.4
The problem occurs rarely [not frequently] - only few row gets auto deleted.
There is neither any cron nor any action in Controller-file to execute "Delete" command/query.
But why this auto-deletion is happening is miserable for me till now.
The Table structure is as provided below:
CREATE TABLE IF NOT EXISTS `bulk_orders` (
`id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`),
UNIQUE KEY `order_no` (`order_no`)
) EN
GINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=16270 ;
CREATE TABLE IF NOT EXISTS `bulk_order_lines` (
`id` int(10) NOT NULL auto_increment,
`order_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=34711 ;
Auto delete query is executed like below :
Delete from bulk_orders where ID=1234;
Can anybody please help me to find the solution on this?
Your question is very vague.
I assume you have baked your CRUD controller actions and templates via bake?
Usually this is the developers fault then - failure by design.
The HTTP specs are pretty clear about what protocol type to use for modifying the DB: POST
Additionally, it should also be secured via auth or some additional measure to avoid abusing.
But using POST here instead of GET prevents BOTs and people with little knowledge of deleting your content - by accident or purpose.
$this->Form->postLink()
is what you should be using.
And the action itself should contain
$this->request->allowMethod('post');
etc.
Please confirm that this is what you got in your code already.
wp_users is creating users but without an ID in the table.
Make sure your settings for ID looks like this in MySql and ensure AUTO_INCREMENT is ticked.
Did you create wp_users manually? The ID column should be defined as auto_increment by default; extracting from one of my databases shows:
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
Try altering the column as described in this question.
Might be worth checking that the other tables have the correct structure as well (auto_increment, indices, etc). The correct definitions can be found in schema.php, here.
It is auto incremented.
No need to define when you create the users.
for the more info, please visit the wordpress.com for any help.
I'm rather new to SQL Server, (did learn SQL back in late 1980's, DB2 if I recall) Today I'm integrating my database layer into SQL, to begin with, SQL Server.
To begin with. As I do today, I will generate in runtime every databases objects, tables objects and indexes programmatically as I do with almost every visual and data object in my projects. That is, I use the visual designing tools very limited.
Every column in my project has a external description file's (every user has profile which contains these files), just as I do with database key's and for visual objects as for effect's as positioning, length, picture-mask, font size, etc. etc. i.e. dynamic forms. Almost every window, grids, filters is created in runtime just as far most of my database connections.
I did build a small test "machine" to create tables in this environment and did well, very easy to create tables within program (I use delphi and ADO)
The problem I encounter is when I flag a column as "autoincrement" or as Identity in SQL Server or if I describe a column as primary key, then SQL Server Management Studio creates automatically a index or key.
That would be ok if I could manage the name it gives this index or key.
Example of this situations:
AdoCommand.CommandText := Str_SQL;
TRY
AdoCommand.Execute;
FINALLY
NotiFy_TimeOut ('Table was created', wait_for_one_sec);
END;
My database engine creates this SQL script which I pass into the string Str_SQL above:
CREATE TABLE GENGITFL
(
NR INT NOT NULL IDENTITY,
GJM CHAR(3) NOT NULL,
HEITI VARCHAR(25) NOT NULL,
KAUPG REAL NULL,
SOLUG REAL NULL,
TOLLG REAL NULL,
DUMMY VARCHAR(20) NULL,
UNIQUE (GJM),
PRIMARY KEY (GJM)
)
SQL Server creates these two indexes automatically :
PK__GENGITFL__C51F17260A463F49
UQ__GENGITFL__C51F17277FA3E6E6
I don't want to use these names for these files, I would prefer names as:
IDX_GENGITFL_GJM
IDX_GENGITFL_NR
The reason should be obvious in light of my intro, the runtime engine can't create these names automatically and I consider it not a option to look for what index files are available within system database. If my external description say there should be index, I would like just to create names for the index automatically by using the prefix, IDX_ next the table name and last the field name or name's with underscore between, as IDX_GENGITFL_GJM etc.
Hope someone understand my poor english and presentation.. I'm rather rusty in english.
Thanks
Edit: After help from marc_s my SQL "script" is like this:
CREATE TABLE GENGITFL
(
NR INT NOT NULL IDENTITY,
GJM CHAR(3) NOT NULL,
HEITI VARCHAR(25) NOT NULL,
KAUPG REAL NULL,
SOLUG REAL NULL,
TOLLG REAL NULL,
DUMMY VARCHAR(20) NULL,
CONSTRAINT IDX_GENGITFL_NR UNIQUE (NR),
CONSTRAINT IDX_GENGITFL_GJM PRIMARY KEY (GJM),
)
CREATE INDEX IDX_GENGITFL_HEITI ON GENGITFL (HEITI)
Thanks again.
If you don't want the system default names - then just specify your own! :
CREATE TABLE GENGITFL
(
NR INT NOT NULL IDENTITY,
GJM CHAR(3) NOT NULL,
HEITI VARCHAR(25) NOT NULL,
KAUPG REAL NULL,
SOLUG REAL NULL,
TOLLG REAL NULL,
DUMMY VARCHAR(20) NULL,
CONSTRAINT IDX_GENGITFL_NR UNIQUE (GJM),
CONSTRAINT IDX_GENGITFL_GJM PRIMARY KEY (GJM)
)
See those CONSTRAINT (yourownnamehere) before the UNIQUE and the PRIMARY KEY ?
Now, your constraints are named as you defined.
I have a database with the following tables:
CREATE TABLE `visitors` (
`name` varchar(64) not null,
`id` int(10) unsigned not null auto_increment,
# ...more fields here
PRIMARY KEY (`id`),
UNIQUE KEY (`name`)
);
CREATE TABLE `credentials` (
`id` int(10) unsigned not null auto_increment,
`visitor_id` int(10) unsigned,
`type` enum('password','openid','google','facebook') not null,
`token` char(40) not null,
`modified` datetime,
`hint` varchar(64),
PRIMARY KEY (`id`),
KEY `visitor` (`visitor_id`),
KEY `token` (`token`)
);
After thinking about this for a awhile, I've decided that this is "right" for e.g. normalization and allowing visitors to have multiple login credentials, including multiple passwords.
However, I'd like to use Cake's ACL features, and AuthComponent assumes that hashed passwords are stored in the same table as user (visitor) information. What's the best way to work around this? Do I have to use Auth->login(), or is there a better way?
If you're using CakePHP 2.x this can be archived pretty simple by creating a new authentication handler. Looking at the existing Form handler will also help you. You should be able to extend it or copy the code into a new handler and modify it to match your needs.
Read the authentication chapter in the book. It has a section just about creating a custom handler but to get an understanding of how the whole thing works I really suggest you to read the whole page.
I want to build an employees table using SQL SERVER 2008 , and in my table I want to have an ID for each employee .
I heared about GUID and I kind of understood that its a data type , But I couldn't use it
could you please show me the way to use it ...
by the way , lets say I want something like this :
CREATE TABLE Employees (
ID guid PRIMARY KEY,
Name NVARCHAR(50) NOT NULL
)
How can I do it ?? because I want to benefit from it , but I couldn't find out how to do that
It's not called GUID in SQL Server. It's called uniqueidentifier
The type is called UNIQUEIDENTIFIER as others have pointed out already. But I think you absolutely must read this article before proceeding: GUIDs as PRIMARY KEYs and/or the clustering key
They are called uniqueidentifier
Don't use uniqueidentifier as primary key if clustered (which is the default for PKs)
Seriously: use a standard IDENTITY instead
You can also consider using NEWSEQUENCIALID as the default value for your ID column as it would be faster than using NEWID() generate the GUIDs.
BUT (from the same link above):-
If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID.
Practical demo, FWIW
DECLARE #guid1 AS uniqueidentifier
SET #guid1 = NEWID()
SELECT #guid1
The GUID in sql server is known by UNIQUEIDENTIFIER data type. below is the desired code.
CREATE TABLE Employees
(
Id UNIQUEIDENTIFIER PRIMARY KEY,
Name NVARCHAR (50) not null
)
GO