PacktPublishing / Developing-Extensions-for-Joomla-5

Developing Extensions for Joomla! 5, published by Packt
MIT License
7 stars 4 forks source link

Chapter 1 "Creating tables in the database" #6

Open SumCompanyInc opened 6 months ago

SumCompanyInc commented 6 months ago

Just a Note.. first off, really appreciate the effort and the book, been trying to develop in joomla for a many many years.

and apologies, I'm still beginner of beginners.. i did read the book through once already, but now on the 2nd go around, and trying to put in some of the code etc.

looking to create the projects table, i got a lot of errors from mysql, it didnt like a lot of the way the book puts it.

  1. It did not like the single quotes anywhere
  2. it mentioned a deprecation for the int length for int(11)
  3. it did not like the DATETIME DEFAILT NOW()

the following is the table creation query that finally took (i also went ahead and made the types consistent with the way phpmyadmin shows the cases)

CREATE TABLE IF NOT EXISTS #__spm_projects (
    id int UNSIGNED NOT NULL AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    alias varchar(255) NOT NULL,
    description TEXT,
    deadline datetime,
    category int,
    created datetime DEFAULT CURRENT_TIMESTAMP,
    modified datetime DEFAULT CURRENT_TIMESTAMP,
    created_by int NOT NULL,
    modified_by int NOT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB;
fred-dinkler commented 5 months ago

The quotes around column names aren't single quotes, they're backquotes, aka backticks. To differentiate a column name from an SQL reserved word, use the back-ticks around the column name. Normal single quotes are for static string values.

The editor used in this forum interpretes backticks as code blocks, so I can't demonstrate here, but look closely at the original code, and you'll see the difference.

It's accepted good practice to always out backticks around column names for readability. Refer to the MySQL manual for your version, or google it.
Atlassian has a good article on the topic. (https://www.atlassian.com/data/sql/single-double-quote-and-backticks-in-mysql-queries)