CakePHP 3 - Save function not generating primary key for database - cakephp-3.0

I have an application I'm working on with Cake 3 and I've baked a bunch of things to get the lay of the land as I'm newer to Cake.
When going to the baked add view for one of my models, filling out the form and submitting, the entry is added to the database but the primary key is blank on the entry. I've been doing some research and in the table class for Cake, I know that it's supposed to be created during the save method. However, I haven't overridden save or anything like that, so I'm a little confused as to why it's not generating the ID field for the primary key.
I was hoping someone might be able to point me in the right direction towards some steps that might help me debug this. Thanks!

From the Cookbook 3.x:
Rather than using an auto-increment key as the primary key, you may also use char(36). CakePHP will then use a unique 36 character UUID (Text::uuid) whenever you save a new record using the Table::save() method.
Try converting you id field in your MySQL table from
VARCHAR(36)
to
CHAR(36)

Related

PowerApps: Access-like update query

I’m pretty new to PowerApps and need to migrate an Access database over to PowerApps, first of all it’s tables to Dataverse. It’s a typical use case for a model-driven app, with many relationships between the tables. All Access tables had an autogenerated ID field as their primary key.
I transferred all tables via Excel ex/import to Dataverse. Before importing,I renamed all ID fields (columns) to ID_old and let Dataverse create its own, autogenerated ID field for each table.
What I want to achieve is to re-establish all relationships between the tables, where the foreign key points to the new primary key provided by Dataverse, as I want to avoid double keys. As a first step I created relationships between the ID_old field and the corresponding (old) foreign key field in the related table.
In good old Access, I’d now simply run an update query, filling the new (yet empty) foreign key field with the new ID of the related table. Finally, I would change the relationship to the new primary and foreign keys and then delete the old ID fields.
Where I got stuck is the update query. I searched the net and found a couple of options like UpdateIf / Patch functions or Power Query or Excel ex/import and some more. They all read pretty complicated and time intensive and I think I must have overseen a very simple solution for such a pretty common problem.
Is there someone out there who might point me in the right (and simple) direction? Thanks!
A more efficient approach would be to start with creating extra ID columns in Access. Generate your GUIDs and fix your foreign keys there. This can be done efficiently using a few SQL update statements.
When it comes to transferring your Access tables to Dataverse you just provide your Access shadow primary keys in the Create message.
I solved the issue as follows, which is pretty efficient in my perception. I”m assuming you have a auto-numbered ID field in every Access table, which you used for your relationships
Export your tables from Access to Excel.
Rename your ID fields to ID_old in all tables using Excel, as well as your foreign key fields to e.g. ForeignKey_old. This will make it easy to identify the fields later in Dataverse.
Import into Dataverse, using the Power Query tool. Important: Make sure, that you choose ID_old as additional primary key field in the last import step.
Re-create all relationships in Dataverse, using the Lookup datatype. This will create a new, yet empty column in your table.
Now use the “Edit in Excel” feature to open your table in Excel. You should get your prefix_foreignkey_old column with the old foreign keys displayed, as well as the reference to your related table, e.g. prefix_referencetable.prefix_id_old, which is still empty.
Now just copy the complete prefix_foreignkey_old column values into the prefix_referencetable.prefix_id_old column.
Import the changes and you’re done.
Hope this is helpful for some of you out there.

Does MySQL database require a unique identifier?

I'm really new to databases in general but could use some advice. I have a database that has about 6000 records (and growing but not crazy amounts). I'd like to build an API so that I can retrieve a property's price history, but have been advised I need a unique ID but I'm not so sure. Can anyone advise?
DB looks like this:
address
price
date_created
status
Address one, main street
£150,000
13/10/2022
new data
Address one, main street
£140,000
16/10/2022
update data
Address two, side road
£350,000
13/10/2022
new data
Maybe you will need to add/edit your data using a CRUD, maybe reference other tables, make foreign keys, etc. I recommend that you add a primary key. I've never worked on tables without a primary key.
I can hear my database teacher in 2001: you should always have a primary key and another unique key apart from the primary one (compound or on a single column) if you don't want your table to be just another excel sheet. And I remember that it was a mathematical demonstration of this rule with injective functions.
Of course, you can ignore these rules, but they are best practice advice for your future self :)
You might find an integer primary key useful, but it's not mandatory.
If the address is sufficient to be the unique column by which you can reference any row, then that's fine. It's called a "natural key." In practice, most of us have experienced that any column you think should be unique ends up not being 100% unique eventually. So a lot of developers recommend adding a "pseudokey" which has no reason not to be unique.
I wrote a chapter called "ID Required" describing the pros and cons of pseudokeys and natural keys in my book SQL Antipatterns, Volume 1: Avoiding the Pitfalls of Database Programming.

Creating a custom Serial Number in Primary Key in mvc4

I am building a website in mvc4 and i want to ask that as i am registering a new user than the user-profile table automatically updated with user-id as auto increment in the 1,2,3,4,5,...sequence but i want this primary key to auto-increment in some other way such as psk1,psk2,psk3...................can i have this ?? if Yes how it is possible?
I have try using custom membership provider but i am not successful by using that method so can any one have better option other than this?
I can think of three ways of creating unique primary keys
Autoincrement by identity.
This is the method that you are already familiar with, leading to automatic generation of primary keys of type 1,2,3,....
Using UUID.
This is a global unique identified which you can create in .NET and pass it as value in your database record.
Custom creation of primary key
If there is a special need, you could set your data table column to be primary Key of type varchar and create either in the .NET domain, or in the SQL server using a trigger. There, you could find the last inserted ID, parse it, increment the integer part and form the new unique primary key. .
Hope I helped!

MySQL Primary Key Field Type

I am working with some transactional data and having issues with my primary key field. When importing data from my order manager system and Google analytics, the OrderID field has the following two value formats:
123456
002-0130775-1483424
How do I format this field to be a primary key?
Thank you in advance for your help! :)
(Running MySQL 5.5.27)
You should create an auto-increment primary key field that contains arbitrary integers (an 'internal' key)... you can put your actual "key" data in another field titled OrderNumber or something similar and put a unique index on it.
Yes, keeping your primary key internal helps insulate you from change. Anything that could be exposed as data should be re-considered as a key.
As an option, you can set up a syntetic primary key (for your internal references only) and an indexed CHAR(20) column (probably with a unique index attached for consistency purposes) – this appears to be a better solution as one day you might want to integrate with another system which might have collisions with the existing data.
See, primary key should be bigint in nature, However you can modify the way you want.
MYSQL 5.5.27 have some important changes from its earlier version ,please follow the link
https://dev.mysql.com/doc/refman/5.5/en/news-5-5-27.html

Best Way to Handle Zero to One (Optional) Foreign Key Relationships in MySql

Not sure if this is possible... however I have a table called "LineItems" with a column called "PackageId" -> PackageId IS optional, however i'd still like to somehow setup a foreign key relationship to its relating table Packages - is this possible? If so how might I go about doing it
Also, I will be using the ADO.net Entity Framework Model v4 in conjunction with MySql. i would like to apply this constraint via MySQL (if possible) and have it carry into the Entity Framework model code
thanks!
Loren
I apologize for my ignorance, however I just learned that by setting a column to allow null appears to allow me to still keep the foriegn key relationship