Alpha DevCon 2018
Results 1 to 18 of 18

Thread: Multi Tenant Auto Increment

  1. #1
    Member
    Real Name
    Beau Woods
    Join Date
    Jan 2017
    Posts
    5

    Default Multi Tenant Auto Increment

    Hi All,
    I am looking to create a multi tenant application, i have a settings table which will store the increment number for orders, invoices etc for each tenant. When saving a new order in a dialog, how do i lookup the next order id and increment that number in the settings table?
    Thanks,

    Beau

  2. #2
    Member
    Real Name
    Sean OKelly
    Join Date
    Sep 2015
    Location
    Charleston, WV
    Posts
    117

    Default Re: Multi Tenant Auto Increment

    Are you trying to create a system where the ID is 1,2,3,4 etc. for each tenant? So, the first order ID for each tenant is 1, the second is 2, etc? In other words, it's not a unique ID, but a sequential "order" number by tenant?

  3. #3
    Member
    Real Name
    Beau Woods
    Join Date
    Jan 2017
    Posts
    5

    Default Re: Multi Tenant Auto Increment

    Hi
    Yes sequential order for each tenant. So tenant 1 could have 1,2,3,4 etc another could have 9001,9002,9003 etc. I can do it on desktop but the webside is stumping me

  4. #4
    "Certified" Alphaholic CharlesParker's Avatar
    Real Name
    Charles Parker
    Join Date
    Dec 2012
    Location
    New Orleans, LA
    Posts
    1,862

    Default Re: Multi Tenant Auto Increment

    Lots of variables here however, if you mean that each user in the multi-tenant application might create invoices and you want to auto-increment the invoice number to the NEXT number - whatever that might be per tenant, then THAT would be done by doing a lookup to the table and then assigning that returned result to the field.
    I use Mysql as a database and this code auto-increments the invoice number by one after getting the MAX value for the tenant in the invoice number column.
    Code:
    function maxinv as c (e as p)
    	'debug(1)
    	dim cn as sql::Connection
    	dim result as l = .f.
    	dim args as sql::Arguments
    	dim rs as sql::ResultSet
    	dim sqlstm as c 
    	dim invnum as n
    	invnum = session.coid
    	
    	result = cn.open("::Name::Connection1")
    	if result then
    			sqlstm = "SELECT MAX(Invoice_Number) FROM company_invoices WHERE company_id = :argcoid"
    			args.Set("argcoid",invnum)
    			result = cn.execute(sqlstm,args)
    				if result then 
    				rs = cn.ResultSet
    				invnum = rs.data(1)
    				invnum = invnum+1
    				e._set.InvoiceNumber.value = invnum
    	end if
    	cn.close()
    end if
    SO the idea is I get the tenant ID (session.coid) and lookup the MAX value in the table and add one then set the field value to the returned and final value.

    I do not know for sure if that is what you are looking for but if it is you would still need to describe your issue better such as what database your using. I was given help in figuring this out myself!
    NWCOPRO Nuisance Wildlife Control Software-My Application: http://www.nwcopro.com "I am not discouraged, because every wrong attempt discarded is another step forward."

  5. #5
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Multi Tenant Auto Increment

    you should consider when in a multi user environment there is a possibility that the increment number may be duplicated when you get the last number and manually increment that number, this does not happen when you use sql server generated auto increment number. if you want to reduce that possibility then you should get the max number and immediately insert the new record. this will reduce but not eliminate the duplication, i think.
    since the id field is only a record number and no way will influence the customer and there is no use for them and they probably will not see it anyway why not just do it sql server generated autoincrement number?
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  6. #6
    "Certified" Alphaholic CharlesParker's Avatar
    Real Name
    Charles Parker
    Join Date
    Dec 2012
    Location
    New Orleans, LA
    Posts
    1,862

    Default Re: Multi Tenant Auto Increment

    Could be true if you have multiple users of a tenant entering invoices!
    They could grab the MAX record at the same time and submit a duplicate, me personally I see no reason for concern because no matter what the invoice is tied to the customer with a UID as well at least in my case. I also warn users that it is possible to create a dupliate number AND they can manually enter in whatever number they want including letters! SO it is entirely up to them. The auto-increment is set when they click the button for NEW invoice only, so after that they can edit it. I guess you could get the max invoice number and then save and submit - and not close the record so as to ensure youv'e got a pretty good chance of creating a unique number, but again, it is really all about how you decide what works for you - like Ghandi said. Seems like a decent method, but I disagree on the auto-increment at the table level due to gaps.


    I don't think auto-increment at the table level for an invoice number is a good idea in a multi-tenant database setup. I don't think it's a good idea, even in a single user setup - you will have gaps in the numbers either way on delete. I guess it's really up to you how you handle it.
    NWCOPRO Nuisance Wildlife Control Software-My Application: http://www.nwcopro.com "I am not discouraged, because every wrong attempt discarded is another step forward."

  7. #7
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Multi Tenant Auto Increment

    in a good accounting system there are no deletes only adjustments. i know quickbooks lets you do it ( i use the product, but not delete). when you delete it takes away the audit trail. in accounting, just like in time, it marches one way.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  8. #8
    "Certified" Alphaholic CharlesParker's Avatar
    Real Name
    Charles Parker
    Join Date
    Dec 2012
    Location
    New Orleans, LA
    Posts
    1,862

    Default Re: Multi Tenant Auto Increment

    Good point on the audit trail.
    NWCOPRO Nuisance Wildlife Control Software-My Application: http://www.nwcopro.com "I am not discouraged, because every wrong attempt discarded is another step forward."

  9. #9
    Member
    Real Name
    Sean OKelly
    Join Date
    Sep 2015
    Location
    Charleston, WV
    Posts
    117

    Default Re: Multi Tenant Auto Increment

    It seems to me that we're over thinking this Beau. I don't see, given the example, any real reason to "store" the sequential number. What it seems to me we want is to "show" a sequential number. In this case, we know order 12002 is after 11997, both of which are orders related to customer id 2343. So, when I query customer id 2343 I read the returned list in an array and order the array and insert a new variable into the array that is computed. I only need to code this function once, and call whenever I want to order sequentially. You could even then push this new data into a list and sort by the new column. This would prevent you from having to worry about writing anything to the server. Hopefully this makes sense.

  10. #10
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Multi Tenant Auto Increment

    i do not agree. you don't have to show an imaginary number by inserting an arbitrary value just to show. you can actually set the invoice number to a real sequential number by using a trigger if you use sql server of some flavor.
    the general trigger statement will be

    Code:
    DROP TRIGGER `add_acctno`;
    
    CREATE DEFINER=`root`@`localhost` TRIGGER `add_invoiceno` BEFORE INSERT ON `customer`
    FOR EACH ROW SET NEW.invoice_number = (SELECT MAX(invoice_number) FROM customer) + 1;
    replace any value to match your table.

    edit: i haven't tested this in a multi tenant situation.
    an alternate approach can be to create a folder for each company and add the tables to each folder then auto increment and uniqueness will be natural.

    here is an example:
    https://www.screencast.com/t/CRaeAsexj43


    corrected for multi tenant multiuser scenario
    Code:
    DROP TRIGGER `add_acct_number`;
    
    CREATE DEFINER=`root`@`localhost` TRIGGER `add_acct_number` BEFORE INSERT ON `customer`
    FOR EACH ROW SET NEW.account_number = (SELECT MAX(account_number) FROM customer WHERE NEW.company_id = company_id)+1;
    https://www.screencast.com/t/m1icXUFLP
    Last edited by GGandhi; 10-24-2017 at 07:42 AM.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  11. #11
    Member
    Real Name
    Sean OKelly
    Join Date
    Sep 2015
    Location
    Charleston, WV
    Posts
    117

    Default Re: Multi Tenant Auto Increment

    Hi Gandhi,

    I'm not saying to not use normal triggers and sequential keys in the database., I'm saying there isn't a need to write to the database a separate number when the primary key of the record is already being done anyway. If he must create a new column to store this value then what you are recommending is a good way to do it.

  12. #12
    Member
    Real Name
    Beau Woods
    Join Date
    Jan 2017
    Posts
    5

    Default Re: Multi Tenant Auto Increment

    Thanks all for your help, i will try to go through these when i return to the office. My reasoning for wanting to store value in a setup table is that some customer like to set there own invoice number at the beginning of each year, i thought it would be easier to just store the next value in another table and when they want to change it, just have a form for this.

  13. #13
    Moderator
    Real Name
    Alan Buchholz
    Join Date
    Oct 2000
    Location
    Delavan, Wisconsin
    Posts
    9,479

    Default Re: Multi Tenant Auto Increment

    So use an internal autoincrement field to set a unique value for your invoice header table in your database. Then the linkages to children will use that. Some call it SID or TransId.

    Make another field called invoice_number that uses a field on the customer table called Next_Invoice_Number (for that customer) and increment it when a new invoice_number is used for that customer.
    Al Buchholz
    Bookwood Systems, LTD
    Weekly QReportBuilder Webinars Thursday 1 pm CST

    Occam's Razor - KISS
    Normalize till it hurts - De-normalize till it works.
    Advice offered and questions asked in the spirit of learning how to fish is better than someone giving you a fish.
    When we triage a problem it is much easier to read sample systems than to read a mind.

  14. #14
    Member
    Real Name
    Sean OKelly
    Join Date
    Sep 2015
    Location
    Charleston, WV
    Posts
    117

    Default Re: Multi Tenant Auto Increment

    Seems like using javascript you could set a default value in the new field of 1 (if no other records exist) when you create a new record. If a record(s) exists, cycle through them to find the max value, and use that plus 1 as the default. Your list is basically an array than can easily be cycled through. I already use something similar to this in another project and it works well. However, if you have multiple users editing underneath a particular tenant, at the same time, then you need to use a server side database trigger. Lot's of ways to handle your request Beau.

  15. #15
    Member
    Real Name
    David Fricke
    Join Date
    Sep 2011
    Location
    Kenosha, WI
    Posts
    369

    Default Re: Multi Tenant Auto Increment

    Hey guys! Thanks for this. Just what I was looking for.

    Question on a modification. Freshbooks.com allows you to specify the invoice number and it will increment based on what you selected. Similar to this. However, they allow you to put a letter in it such as 2001A and then the next invoice will then be something like 2001B, etc. It will switch to letters and increment those until someone changes the next invoice back to something else.

    Anyone seen anything like this or can this be done by modifying this? Or am I thinking way above my pay level. :)

  16. #16
    Member
    Real Name
    Sean OKelly
    Join Date
    Sep 2015
    Location
    Charleston, WV
    Posts
    117

    Default Re: Multi Tenant Auto Increment

    If you are going to use the trigger concept on the database server, then it must store the id as a string - keep that in mind. If the user started by inputting an id, rather than using an auto generated one, then you use javascript. It's easy to do a javascript function to see if the resulting string evaluates to a number. If it doesn't evaluate to a number, check the last character, and if it doesn't evaluate to a number, then find the next character and use it to replace the existing character. The new string is now your id. I'm leaving some things out that I would do, but this is generally the idea and fairly easy to implement.

    Quote Originally Posted by dfricke10 View Post
    Hey guys! Thanks for this. Just what I was looking for.

    Question on a modification. Freshbooks.com allows you to specify the invoice number and it will increment based on what you selected. Similar to this. However, they allow you to put a letter in it such as 2001A and then the next invoice will then be something like 2001B, etc. It will switch to letters and increment those until someone changes the next invoice back to something else.

    Anyone seen anything like this or can this be done by modifying this? Or am I thinking way above my pay level. :)

  17. #17
    "Certified" Alphaholic
    Real Name
    eric
    Join Date
    Mar 2009
    Location
    Amsterdam
    Posts
    1,284

    Default Re: Multi Tenant Auto Increment

    Quote Originally Posted by WebDesignShop View Post
    If you are going to use the trigger concept on the database server, then it must store the id as a string - keep that in mind. If the user started by inputting an id, rather than using an auto generated one, then you use javascript. It's easy to do a javascript function to see if the resulting string evaluates to a number. If it doesn't evaluate to a number, check the last character, and if it doesn't evaluate to a number, then find the next character and use it to replace the existing character. The new string is now your id. I'm leaving some things out that I would do, but this is generally the idea and fairly easy to implement.
    Other option :

    create two databases,

    One for customer incl. adress etc customer and all other stuff incl. <customer> schema

    Second database where each customer has it's own schema inside the database

    Result there less programming and maintance no conflits because schema is attached to customer schema
    just an idea m2p

  18. #18
    "Certified" Alphaholic
    Real Name
    Gregg Schmidt
    Join Date
    Mar 2001
    Location
    Milwaukee
    Posts
    1,041

    Default Re: Multi Tenant Auto Increment

    I guess I could be over-simplifying things here, but why not use a separate invoice-id table for each tenant ?

Similar Threads

  1. How to bulid multi Tenant application
    By eduduru in forum Database Design
    Replies: 6
    Last Post: 04-11-2016, 03:37 PM
  2. Multi-tenant Web app
    By blancolandau in forum Application Server Version 10 - Web/Browser Applications
    Replies: 2
    Last Post: 12-20-2010, 09:49 PM
  3. Auto-Increment value duplicated in multi-user environment
    By betty in forum Alpha Five Version 10 - Desktop Applications
    Replies: 22
    Last Post: 10-25-2010, 02:50 PM
  4. auto-increment and multi-user
    By SwissCharles in forum Alpha Five Version 5
    Replies: 3
    Last Post: 04-02-2003, 06:02 AM
  5. Auto Increment on a network (multi user)
    By Chris.Tanti in forum Alpha Five Version 4
    Replies: 1
    Last Post: 03-19-2001, 09:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •